[英]Selecting data from MYSQL table based on rows
I have the following sql table: 我有以下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
from this table i want to create another table which looks like this: 我要从此表创建另一个如下表:
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
The SQl fiddle is here: http://sqlfiddle.com/#!2/cf745df/1 SQl小提琴在这里: http ://sqlfiddle.com/#!2/cf745df/1
Any pointers on approach? 关于方法有什么建议吗?
Think this is probably what you are looking for: 认为这可能是您要寻找的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.