现在有一个表,其格式为:
EMPNO ENAME JOB MGR BIRTHDAY SAL COMM DEPTNO其中empno 为员工的编号
ename为员工的姓名
birthday为其生日
现在现在公司为了提高员工的积极性,特地每个月为当月出生的员工过生日,
故需要知道每个月有哪些员工生日,要求即使当月没有过生日的员工,该月也要列出来。现有以下的SQL,
可以满足需要,但觉得不简洁,效率上可能也不高,
各位大虾看一下在这两方面有哪些需要修改的地方:
select a.month,nvl(b.count,0) count from
(select 1 month from dual
union
select 2 month from dual
union
select 3 month from dual
union
select 4 month from dual
union
select 5 month from dual
union
select 6 month from dual
union
select 7 month from dual
union
select 8 month from dual
union
select 9 month from dual
union
select 10 month from dual
union
select 11 month from dual
union
select 12 month from dual
)a,--获得月份列表
(select extract(month from birthday) birmonth,count(empno) count from scott.emp
group by extract(month from birmonth)) b --获得生日月份统计
where a.month=b.birmonth(+)
order by a.month

解决方案 »

  1.   

    参考
    select   a.month,nvl(b.ename,0)   count   from 
    (select   1   month   from   dual 
    union 
    select   2   month   from   dual 
    union 
    select   3   month   from   dual 
    union 
    select   4   month   from   dual 
    union 
    select   5   month   from   dual 
    union 
    select   6   month   from   dual 
    union 
    select   7   month   from   dual 
    union 
    select   8   month   from   dual 
    union 
    select   9   month   from   dual 
    union 
    select   10   month   from   dual 
    union 
    select   11   month   from   dual 
    union 
    select   12   month   from   dual 
    )a,
    (select decode(to_char(HIREDATE,'MM'),'01',1,'02',2,'03',3,'04',4,'05'
    ,5,'06',6,'07',7,'08',8,'09',9,'10',10,'11',11,'12',12) mon,ename from emp) b
    where a.month=b.mon(+)
    order by a.month只是不能满足"要求即使当月没有过生日的员工,该月也要列出来"。
      

  2.   

    不好意思弄错了,上面的已经满足了"即使当月没有过生日的员工,该月也要列出来""select   decode(to_char(HIREDATE,'MM'),'01',1,'02',2,'03',3,'04',4,'05' 
    ,5,'06',6,'07',7,'08',8,'09',9,'10',10,'11',11,'12',12)   mon,ename   from   emp这样才是不满足"即使当月没有过生日的员工,该月也要列出来"