-1

特定のプロジェクトについて、9 か月分の日付が格納されるテーブルが 1 つあります (以下を参照)。

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

私は、体重が数か月間しか存在しない別のテーブルを持っています。最終出力を次のようにしたいです(2番目のテーブルにデータが存在しない場合は、すべての月と重量を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 に答える