【问题标题】:Grouping and sorting data month wise sorted data showing month and year in ascending order分组和排序数据按月排序的数据按升序显示月份和年份
【发布时间】:2014-10-30 06:17:50
【问题描述】:

我需要按月获取数据并进行排序,为此我尝试了以下查询

select to_char(regn_date,'Mon-yyyy') "Month", count(id) "No of Persons" 
from Person  
group by to_char(regn_date,'Mon-yyyy')  
order by to_char(regn_date,'Mon-yyyy')  

我得到的输出是

月人数

Dec-2011    1383  
Feb-2012    1230  
Jan-2012    1409  
Mar-2012    1495  
Nov-2011    985  
Oct-2011    825  
Sep-2011    742  

对字符串值进行升序排序,即按 to_char(regn_date,'Mon-yyyy') asc 排序

select to_char(regn_date,'Mon-yyyy') "Month", count(id) "No of Persons" 
from Person  
group by to_char(regn_date,'Mon-yyyy')  
order by to_char(regn_date,'Mon-yyyy') asc 

月人数

Dec-2011    1383  
Feb-2012    1230  
Jan-2012    1409  
Mar-2012    1495  
Nov-2011    985  
Oct-2011    825  
Sep-2011    742  

获取数据排序后的日期格式显示不同

 select to_date(to_char(regn_date,'MM-YYYY'),'MM-YYYY') "Month", count(id) "Number  of  Persons" from Person    
 where trunc(regn_date) < '31-MAR-2012'  
 group by to_date(to_char(regn_date,'MM-YYYY'),'MM-YYYY')  
 order by to_date(to_char(regn_date,'MM-YYYY'),'MM-YYYY')asc  

结果:
月人数

9/1/2011    742    
10/1/2011   825   
11/1/2011   985    
12/1/2011   1383    
1/1/2012    1409    
2/1/2012    1230    
3/1/2012    1495   

所需的输出是按日期排序的数据,按升序显示月份和年份。

月人数

Sep-2011    742    
Oct-2011    825       
Nov-2011    985    
Dec-2011    1383    
Jan-2012    1409    
Feb-2012    1230     
Mar-2012    1495   

【问题讨论】:

    标签: sql database sorting oracle-sqldeveloper


    【解决方案1】:

    使用TO_CHAR进行显示,使用TO_DATE进行操作。

    SQL> WITH DATA AS(
      2  SELECT 'Dec-2011' mnth,  1383 persons FROM dual UNION ALL
      3  SELECT 'Feb-2012' ,   1230   persons FROM dual UNION ALL
      4  SELECT 'Jan-2012',    1409   persons FROM dual UNION ALL
      5  SELECT 'Mar-2012',    1495   persons FROM dual UNION ALL
      6  SELECT 'Nov-2011',    985   persons FROM dual UNION ALL
      7  SELECT 'Oct-2011',    825   persons FROM dual UNION ALL
      8  SELECT 'Sep-2011',    742   persons from dual
      9  )
     10  SELECT to_char(to_date(mnth,'Mon-YYYY'), 'Mon-YYYY') mnth, persons
     11    FROM DATA
     12  ORDER BY to_date(mnth,'Mon-YYYY')
     13  /
    
    MNTH                  PERSONS
    -------- --------------------
    Sep-2011                  742
    Oct-2011                  825
    Nov-2011                  985
    Dec-2011                 1383
    Jan-2012                 1409
    Feb-2012                 1230
    Mar-2012                 1495
    
    7 rows selected.
    
    SQL>
    

    EDIT WITH 子句用于构建示例表,仅用于演示。在您的数据库中,您只需用您的表名重命名 DATA 并执行选择查询。

    所以,你的最终查询看起来像,

    SELECT   to_char(to_date(regn_date,'Mon-YYYY'), 'Mon-YYYY') regn_date, 
             count(id) "No of Persons"
      FROM   person
    GROUP BY to_date(regn_date,'Mon-YYYY')
    ORDER BY to_date(regn_date,'Mon-YYYY')
    /
    

    考虑到regn_dateliteral 的输入。

    如果regn_datedate,那么,

     SELECT  to_char(regn_date,'Mon-YYYY') regn_date, 
             count(id) "No of Persons"
      FROM   person
    GROUP BY to_char(regn_date,'Mon-YYYY')
    ORDER BY regn_date
    /
    

    【讨论】:

    • WITH DATA AS( ) 子句包含硬编码值,如何使这个查询通用?
    • 只需删除WITH 子句,并用DATA 代替PERSONS 作为表名。
    • SQL> SELECT to_char(to_date(sysdate,'Mon-YYYY'), 'Mon-YYYY') from dual;导致错误。 ORA-01843:无效月份
    • select to_char(sysdate, 'Mon-YYYY') from dual;
    • 您的问题得到解答了吗?请将其标记为已回答。
    【解决方案2】:

    只按实际日期排序,不转成字符串:

    SELECT to_char(regn_date,'Mon-yyyy') "Month", count(id) "No of Persons" 
    FROM Person  
    GROUP BY to_char(regn_date,'Mon-yyyy')  
    ORDER BY regn_date
    

    【讨论】:

    • 嗨@bilalhaider,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
    【解决方案3】:

    截断月份的日期,然后将其转换为 MON-YYYY 格式就可以了。

    SELECT to_char(TRUNC(regn_date, 'mm'),'MON-YYYY','nls_date_language=american'),count(id)        FROM        Person    
    GROUP BY TRUNC(regn_date, 'mm')  
    order by TRUNC(regn_date, 'mm')  
    

    我得到了所需的输出排序和分组。谢谢你的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 2023-04-03
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      相关资源
      最近更新 更多