Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this table:
table name: sale
-----------------------------------------<br />
category    |  amount    |   currency   |<br />
-----------------------------------------<br />
cat1        |  10,000    |   USD        |<br />
cat2        |  5,000     |   EUR        |<br />
cat3        |  15,000    |   SAR        |<br />
cat2        |  30,000    |   USD        |<br />
cat1        |  45,000    |   SAR        |<br />
cat3        |  7,000     |   EUR        |<br />
cat2        |  3,000     |   EUR        |



I want a query (sql server) that return this result:


-------------------------------------------<br />
category |  Total_USD |  Total_EUR |  Total_SAR |<br />
-------------------------------------------<br />
cat1     |  10,000    |  0         |  45,000    |<br />
cat2     |  30,000    |  8,000     |  0         |<br />
cat3     |  0         |  7,000     |  15,000    |


What I have tried:

I have tried this:

SELECT  S.[CategoryID]
       ,S.[CategoryName]

       ,(SELECT ISNULL(SUM([Amount]),0)
           FROM SALE
          WHERE [Currency] = 'USD'
            AND SALE.[CategoryID] = S.[CategoryID]) AS 'Total_USD'
            
       ,(SELECT ISNULL(SUM([Amount]),0) 
           FROM SALE
          WHERE [Currency] = 'ERU'
            AND SALE.[CategoryID] = S.[CategoryID]) AS 'Total_ERU'
            
       ,(SELECT ISNULL(SUM([Amount]),0) 
           FROM SALE
          WHERE [Currency] = 'SAR'
            AND SALE.[CategoryID] = s.[CategoryID]) AS 'Total_SAR'
        
        FROM Sale AS S
        GROUP BY S.[CategoryID],S.[CategoryName]


it works for me, but it shows this results:
-------------------------------------------------<br />
category |  Total_USD |  Total_EUR |  Total_SAR |<br />
-------------------------------------------------<br />
cat1     |  10,000    |  0         |  45,000    |<br />
cat2     |  30,000    |  8,000     |  0         |<br />
cat3     |  0         |  7,000     |  15,000    |<br />
cat4     |  0         |  0         |  0         |


what i want is: don't select results that has (0) values, like above in (cat4).
Posted
Updated 30-Dec-22 11:08am
v10
Comments
Dave Kreskowiak 30-Dec-22 19:33pm    
Questions with solutions cannot be deleted. I've rolled back the changes you made to destroy the question.

1 solution

1) DON'T SHOUT! Using all uppercase is considered as SHOUTING on the internet, and rude. You have a proper keyboard, so use proper case.

2) It's not even slightly clear why that data should give those results, and what the "general rules" for achieving what you want might be - and without that, nobody can really help you.

So start out by working out the rules, and test them thoroughly before you even begin to look for a solution.
The start by look at the GROUP BY clause as a subquery to give you the aggregate data I suspect you are going to need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900