有这样一张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
帮帮忙 怎么得到如上格式

解决方案 »

  1.   

    select n.l,cn from
    (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
      

  2.   

    建表 放12月数据,比如表months(birthday  integer)select a.birthday  ,b.cs 
    from months a
    left join (select birthday,count(s#) cs from student group by birthday ) b
    on a.birthday  =b.birthday  
      

  3.   

    没有的用0表示,不知道符合楼主的意思不select distinct 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
      

  4.   


    select n.l,st.cn from  --这里少了写别名