按年月在Oracle SQL查询中基于日期排序的结果

huangapple go评论63阅读模式
英文:

Sorting result based on the date by year followed by month in oracle sql query

问题

Here is the translated code snippet:

我有这个查询来按Month_Year和计数格式分组和显示结果

SELECT
	TO_CHAR (D_DATE, 'MON')
     || '_'
     || TO_CHAR (D_DATE, 'YYYY') Month_Year,
	COUNT (*) Request_Count
FROM
	MY_TABLE WHERE V_TYPE='ABCD'
GROUP BY
	TO_CHAR (D_DATE, 'MON') || '_' || TO_CHAR (D_DATE, 'YYYY')
	
ORDER BY
	TO_CHAR (D_DATE, 'YYYY'), TO_CHAR (D_DATE, 'MON');

The code will now sort the output first by year and then by month as requested.

英文:

I have this query to group and show results in Month_yeat and count format.

SELECT
	TO_CHAR (D_DATE,
	'MON')

         || '_'

         || TO_CHAR (D_DATE,
	'YYYY') Month_Year,
	COUNT (*) Request_Count
FROM
	MY_TABLE WHERE V_TYPE='ABCD'
GROUP BY
	TO_CHAR (D_DATE,
	'MON') || '_' || TO_CHAR (D_DATE,
	'YYYY')
	
ORDER BY
	1;

I require the output should be sorted based on the year first then month.
eg:
The current output is :

MAR_2023	2223
FEB_2023	433
MAY_2022	10
APR_2022	77

Required output is :

FEB_2023	433
MAR_2023	2223
APR_2022	77
MAY_2022	10

I know that this is sorting based on alphabetical if i give the condition in order by, but need a solution which can solve this problem
Note : grouping year and month as separate column and sorting on it is not acceptable.

答案1

得分: 1

你可以这样做,但需要额外添加group byorder by子句,例如:

SELECT TO_CHAR (D_DATE, 'MON') || '_' || TO_CHAR (D_DATE, 'YYYY') Month_Year,
       COUNT (*) Request_Count
  FROM MY_TABLE
 WHERE V_TYPE = 'ABCD'
GROUP BY TO_CHAR (D_DATE, 'MON') || '_' || TO_CHAR (D_DATE, 'YYYY'),
         TO_CHAR (d_date, 'yyyymm')
ORDER BY TO_CHAR (d_date, 'yyyymm')
  • order by 确保行正确排序
  • group by 随后也需要(否则会出现“ORA-00979: not a GROUP BY expression”错误)
英文:

You can, but with additional part of group by and order by clause, e.g.

  SELECT TO_CHAR (D_DATE, 'MON') || '_' || TO_CHAR (D_DATE, 'YYYY') Month_Year,
         COUNT (*) Request_Count
    FROM MY_TABLE
   WHERE V_TYPE = 'ABCD'
GROUP BY TO_CHAR (D_DATE, 'MON') || '_' || TO_CHAR (D_DATE, 'YYYY'),
         TO_CHAR (d_date, 'yyyymm')
ORDER BY TO_CHAR (d_date, 'yyyymm')
  • order by will make sure that rows are correctly sorted
  • group by then requires it as well (otherwise you'll get "
    ORA-00979: not a GROUP BY expression" error)

答案2

得分: 0

将日期截断到月初,按照这个日期分组,然后按截断日期而不是格式化字符串排序。

SELECT TO_CHAR(TRUNC(D_DATE, 'MM'), 'MON_YYYY') 月份_年份,
       COUNT (*) 请求数量
FROM   MY_TABLE
WHERE  V_TYPE='ABCD'
GROUP BY TRUNC(D_DATE, 'MM')
ORDER BY TRUNC(D_DATE, 'MM');

对于示例数据:

CREATE TABLE my_table (v_type, d_date) AS
SELECT 'ABCD', DATE '2022-01-01' + LEVEL FROM DUAL CONNECT BY LEVEL <= 19 UNION ALL
SELECT 'ABCD', DATE '2023-01-01' + LEVEL FROM DUAL CONNECT BY LEVEL <= 12 UNION ALL
SELECT 'ABCD', DATE '2023-02-01' + LEVEL FROM DUAL CONNECT BY LEVEL <=  5 UNION ALL
SELECT 'ABCD', DATE '2023-03-01' + LEVEL FROM DUAL CONNECT BY LEVEL <=  7;

输出:

月份_年份 请求数量
JAN_2022 19
JAN_2023 12
FEB_2023 5
MAR_2023 7

fiddle

英文:

Truncate the date to the start of the month and group by that then order by the truncated date and not the formatted string.

SELECT TO_CHAR(TRUNC(D_DATE, &#39;MM&#39;), &#39;MON_YYYY&#39;) Month_Year,
       COUNT (*) Request_Count
FROM   MY_TABLE
WHERE  V_TYPE=&#39;ABCD&#39;
GROUP BY TRUNC(D_DATE, &#39;MM&#39;)
ORDER BY TRUNC(D_DATE, &#39;MM&#39;);

Which, for the sample data:

CREATE TABLE my_table (v_type, d_date) AS
SELECT &#39;ABCD&#39;, DATE &#39;2022-01-01&#39; + LEVEL FROM DUAL CONNECT BY LEVEL &lt;= 19 UNION ALL
SELECT &#39;ABCD&#39;, DATE &#39;2023-01-01&#39; + LEVEL FROM DUAL CONNECT BY LEVEL &lt;= 12 UNION ALL
SELECT &#39;ABCD&#39;, DATE &#39;2023-02-01&#39; + LEVEL FROM DUAL CONNECT BY LEVEL &lt;=  5 UNION ALL
SELECT &#39;ABCD&#39;, DATE &#39;2023-03-01&#39; + LEVEL FROM DUAL CONNECT BY LEVEL &lt;=  7;

Outputs:

MONTH_YEAR REQUEST_COUNT
JAN_2022 19
JAN_2023 12
FEB_2023 5
MAR_2023 7

fiddle

huangapple
  • 本文由 发表于 2023年4月19日 15:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定