-1

我有一张表,其中存储了 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

那没起效!刚刚返回了匹配的记录。请帮忙!提前致谢!

4

1 回答 1

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;
于 2013-05-23T08:20:56.400 回答