sql查询组按月的日期

时间:2017-12-23 16:05:48

标签: sql oracle

我有一个记录表,其中包含创建的行的日期。 (oracle db)

ID    City       CreateDate
1     city-1     12.12.2017
1     city-2     13.12.2017
1     city-1     13.12.2017
1     city-3     12.12.2017
....
....

我想在一个月内创建一个daiy报告。例如,12月份的城市1报告

Day    Count
1      10
2      80
3      60
4      10
...
30     11

2 个答案:

答案 0 :(得分:2)

我认为你可以使用提取计数函数:

SELECT EXTRACT(day FROM CreateDate) "Day",
      COUNT(CreateDate) "Number of Reports"
      FROM yourTableName
      GROUP BY EXTRACT(day FROM CreateDate)
      ORDER BY "Number of Reports" ASC;

答案 1 :(得分:1)

如果我理解正确,以下查询将生成您想要的12月报告。

SELECT EXTRACT(day FROM CreateDate) "Day", COUNT(*) "Count" FROM your_record_table WHERE EXTRACT(month FROM CreateDate) = 12 GROUP BY EXTRACT(day FROM CreateDate) ORDER BY EXTRACT(day FROM CreateDate);