0
SELECT d_main.Type,ItemGrade,ItmName,d_sub.ItmCode,
       (CASE WHEN d_sub.FulItmCode=item.ItmCode and d_main.Type='E' 
             THEN sum(d_sub.Qty)  ELSE 0 END) as f1,
       (CASE WHEN d_sub.FulItmCode=item.ItmCode and d_main.Type='L' 
             THEN  sum(d_sub.Qty)  ELSE 0 END) as f2
from d_main, d_sub, item
where d_main.FullNo=d_sub.FullNo and
      item.ItmCode = d_sub.ItmCode
group by d_main.Type,d_sub.ItmCode;

出力

d_main.Type ItemGrade       ItmName d_sub.ItmCode   f1      f2  
----------- ---------       ------- -------------   --      --
E           A               AAAAAA  A005            5.000   0
E           B               BBBBBB  B006            1.000   0
E           C               CCCCCC  C015            3.050   0
L           A               AAAAAA  A005            0       5.750
L           B               BBBBBB  B006            0       8.000

期待される結果と私が望む答え

    A       AAAAAA      A005        5.000   5.750
    B       BBBBBB      B006        1.000   8.000
    C       CCCCCC      C015        3.050   0.000
4

2 に答える 2

1

d_main.typeでのグループ化を停止し、(条件付き合計の代わりに)条件付きアイテムを合計します。

SELECT ItemGrade, ItmName, d_sub.ItmCode,
       sum(CASE WHEN d_sub.FulItmCode=item.ItmCode and d_main.Type='E' 
                THEN d_sub.Qty ELSE 0 END) as f1,
       sum(CASE WHEN d_sub.FulItmCode=item.ItmCode and d_main.Type='L' 
                THEN d_sub.Qty ELSE 0 END) as f2
from d_main, d_sub, item
where d_main.FullNo=d_sub.FullNo and
      item.ItmCode = d_sub.ItmCode
group d_sub.ItmCode;
于 2012-10-16T06:50:12.943 に答える
0

これを試して、

GROUP BY d_sub.ItmCode;
于 2012-10-16T06:49:03.233 に答える