简体   繁体   中英

How to add the 2 different column values within a case statement in SQL?

I have a Customer table in which I store how much money is spent by each customer on each product. (Like in the first table given in the attached image)

在此处输入图片说明

What I want is to find how much money is spend for bundle product like in the second table.

This just a sample query I wrote for example

  select EmailAddress 
       , case when Books>0 and Movies>0 then sum(Books + Movies)  end as Books_Movies
       , case when Books>0 and Games>0 then sum(Books + Games)  end as Books_Games
       , case when Movies>0 and Games>0 then sum(Movies + Games) end as Books_Music
       
    from Customers

Any help is really appreciated

you are complicating your solution while it is as simple as this:

select EmailAddress 
       , (Books + Movies) as Books_Movies
       , (Books + Games) as Books_Games
       , (Movies + Games) as Books_Music       
from Customers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM