2012-10-16 94 views
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; 

輸出SQL CASE WHEN STATME在MySQL中沒有回答的答案,我想

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 

回答

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; 
+0

是的我得到了一個錯誤信息說 「錯誤代碼:1111無效使用組功能的」以前的代碼 – Buddhi

+0

非常感謝你的工作。乾杯! – Buddhi

0

試試這個,

GROUP BY d_sub.ItmCode;