显示外连接上的所有记录

时间:2013-05-23 07:04:33

标签: sql database oracle oracle10g

我有一个表格,对于特定项目,存储了9个月的日期(如下所示)

01-01-13    50036    027490101    WO12PS00003
01-02-13    50036    027490101    WO12PS00003
01-03-13    50036    027490101    WO12PS00003
01-04-13    50036    027490101    WO12PS00003
01-05-13    50036    027490101    WO12PS00003
01-06-13    50036    027490101    WO12PS00003
01-07-13    50036    027490101    WO12PS00003
01-08-13    50036    027490101    WO12PS00003
01-09-13    50036    027490101    WO12PS00003

我有另一个表,其中权重仅存在几个月。我希望最终输出像(显示所有月份和wt为0表示第二个表中不存在数据的那些)

  projec     sl      tech_no      mon    yr      wt         ident    dt
027490101    35    WO12PS00003    01    2014    200        50036    01-01-13
027490101    35    WO12PS00003    02    2014    0          50036    01-02-13
027490101    35    WO12PS00003    09    2013    107        50036    01-03-13
027490101    35    WO12PS00003    10    2013    0          50036    01-04-13
027490101    35    WO12PS00003    11    2013    0          50036    01-05-13
027490101    36    WO12PS00003    02    2014    200        50036    01-06-13
027490101    36    WO12PS00003    12    2013    400        50036    01-07-13
027490101    77    WO12PS00003    11    2013    0          50036    01-08-13
027490101    77    WO12PS00003    12    2013    3321       50036    01-09-13

我的疑问是:

select a.projec,sl,a.tech_no,a.mon,a.yr,nvl(sum(a.wt),0) plan_sum ,b.ident,b.dt
  from pp_init_plan a,pp_mon b 
 where a.projec=b.projec 
   and a.sl=b.sl 
   and and to_Char(b.dt(+),'yyyy')=yr 
   and to_char(b.dt(+),'mm') =a.mon
group by a.projec,a.slno,a.tech_no,a.mon,a.yr,b.ident,b.dt 
order by a.tech_no,a.mon,a.yr,b.ident,b.dt

它不起作用!刚刚返回匹配的记录。 请帮忙!提前致谢!

1 个答案:

答案 0 :(得分:1)

为清晰起见,您可以使用SQL92语法:

select
  a.projec, a.sl, a.tech_no, a.mon, a.yr
, nvl(a.wt,0) plan_sum, b.ident, b.dt
from pp_init_plan a
right join pp_mon b
on a.projec=b.projec
  and a.sl=b.sl
  and to_char(b.dt,'yyyy')=a.yr
  and to_char(b.dt,'mm')  =a.mon;