有这样一张student表
s# birthday
1 6
2 7
3 2
4 6
5 7
birthday 我只简写了月份
需求是 查出 各个月份有几个人
比如:
birthday 人数
2 1
6 2
7 2
现在我想让 没有人数的月份也显示出来 1到12,没有的人数列显示空就行
比如:
select birthday,count(s#) from student group by birthday
birthday 人数
1
2 1
3
4
5
6 2
7 2
...后面省略...
12
帮帮忙 怎么得到如上格式
s# birthday
1 6
2 7
3 2
4 6
5 7
birthday 我只简写了月份
需求是 查出 各个月份有几个人
比如:
birthday 人数
2 1
6 2
7 2
现在我想让 没有人数的月份也显示出来 1到12,没有的人数列显示空就行
比如:
select birthday,count(s#) from student group by birthday
birthday 人数
1
2 1
3
4
5
6 2
7 2
...后面省略...
12
帮帮忙 怎么得到如上格式
(select to_char(birthday,'MM') dt,count(s#) cn from a_student group by to_char(birthday,'MM')) st,
(select lpad(rownum,2,'0') l from dual connect by level<13) n
where st.dt(+)=n.l
order by n.l
from months a
left join (select birthday,count(s#) cs from student group by birthday ) b
on a.birthday =b.birthday
,sum(case when 1=1 and birthday='1' then 1 else 0 end) over(partition by birthday) --1月份的
...
,sum(case when 1=1 and birthday='12' then 1 else 0 end) over(partition by birthday) --1月份的
from student order by birthday
select n.l,st.cn from --这里少了写别名