0

I have the following sql table:

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

Any pointers on approach?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. For a moment, i forgot about case statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.