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 回答