我有以下sql表:
MONTH CODE DESCR QTY PRICE
201401 3641 HO 29 3829
201401 3641 IL 2 2
201401 3641 Office 34 43576
201401 3641 Other 38 424
201401 3641503 HO 2904 13584
201401 3641503 IL 2045 2833
201401 3641503 Office 106 237
201401 3641503 Other 79 129
201401 364 HO 37 182
201401 364 IL 8 9
201401 364 Office 750 1317
201401 364 Other 5 6
从这个表我想创建另一个看起来像这样的表:
MONTH CODE HO_qty IL+Office_QTY Other_QTY HO_Price IL+Office_Price Other_Price
201401 3641 29 36(34+2) 38 3829 43578(43576+2) 424
201401 3641503 2904 2151 79 13584 3070 129
201401 364 37 758 5 182 1326 6
SQl小提琴在这里:http://sqlfiddle.com/#!2/cf745df/1
关于进场的任何指示?
答案 0 :(得分:2)
认为这可能就是你要找的东西:
Select
MONTH, CODE,
sum(case when descr in ('Office','IL') then QTY end) as `IL+Office_QTY`,
sum(case when descr = 'HO' then QTY end) as `HO_qty`,
sum(case when descr = 'Other' then QTY end) as `Other_QTY`,
sum(case when descr in ('Office','IL') then PRICE end) as `IL+Office_Price`,
sum(case when descr = 'HO' then PRICE end) as `HO_Price`,
sum(case when descr = 'Other' then PRICE end) as `Other_Price`
from Table1
group by MONTH, CODE