首页 > 解决方案 > 需要 SQL 查询以在 oracle 中打印从 2016 年 11 月到 2018 年 10 月的月份和年份记录数

问题描述

需要 SQL 查询来打印 Oracle 中从 2016 年 11 月到 2018 年 10 月的月和年记录计数。

例子

表 xyz:

Count(*)| Mon-YYYY|
234440  | Nov-2016|
234443  | DEC-2016|
234445  | JAN-2016|
234446  | FEB-2016|
;
;
;
;
234446  | OCT-2018|

标签: sqloracleplsqloracle11g

解决方案


一般来说&简单地写(以斯科特的 EMP 表为例),就像

select 
  to_char(hiredate, 'yyyy.mm') year_month,
  count(*)
from emp
where hiredate between date '2016-11-01' and date '2018-10-01'
group by to_char(hiredate, 'yyyy.mm')
order by to_char(hiredate, 'yyyy.mm');

推荐阅读