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

4

1 回答 1

4

您可以使用 选择加法字段CASE

SELECT DAY, 
       SUM(CASE DAY 
             WHEN 'MONDAY' THEN TOTAL 
             ELSE LAPTOPSOLD 
           END) AS QtySold 
FROM   TBL 
GROUP  BY DAY
于 2013-09-02T16:36:45.523 回答