0

I have such a difficult problem from my knowledge.

I need to write a sole query that perform this action:

write a query that for the day Monday extract the sum of computer sold, and for the rest of the days just the sum of Laptop.

For example I have this table

Day      LaptopSold DesktopSold Total
Monday      2            2        4
Tuesday     2            3        5
Monday      1            1        2
Wednesday   2            2        4
Tuesday     1            4        5

The result should be this:

Day       QtySold
Monday      6
Tuesday     3
wed         2

I can achieve the goal just writing two separate queries with the Group By for the Day field, but in one query for me is impossible!!!

Could you help me, please!!! Thanks in advance Lu

3
  • 1
    How does the Monday value equal six (6)? Shouldn't it be two (2)? Commented Sep 2, 2013 at 16:37
  • 2
    @BellevueBob There is 2 lines that having Monday as day...and for monday you need to look at the total column :) Commented Sep 2, 2013 at 16:46
  • @FabienTheSolution Sheesh, I need more coffee (or glasses)! Commented Sep 2, 2013 at 16:53

1 Answer 1

4

You can select the additive field with a CASE:

SELECT DAY, 
       SUM(CASE DAY 
             WHEN 'MONDAY' THEN TOTAL 
             ELSE LAPTOPSOLD 
           END) AS QtySold 
FROM   TBL 
GROUP  BY DAY
Sign up to request clarification or add additional context in comments.

Comments

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.