Click here to Skip to main content
15,883,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code that I use in SQL for Vb.net values for a specific Department, but now I need all the departments to be shown.

SELECT   SUM(Price)                    AS 'Total'
         ,CONVERT(char(7), date, 120)  AS 'year'
		 ,Department as 'Department'
FROM     [dbo].[Expenses]
WHERE    Department=('Electronics')
GROUP BY  Rollup (CONVERT(char(7), date, 120)), Department  
HAVING   CONVERT(char(7), date, 120) >= '2017-01'  
AND      CONVERT(char(7), date, 120) <= '2020-01' 


I could use the Union to show me the other Departments like here:

SELECT   SUM(Price)                    AS 'Total'
         ,CONVERT(char(7), date, 120)  AS 'year'
		 ,Department as 'Department'
FROM     [dbo].[Expenses]
WHERE    Department=('Electronics')
GROUP BY  Rollup (CONVERT(char(7), date, 120)), Department  
HAVING   CONVERT(char(7), date, 120) >= '2017-01'  
AND      CONVERT(char(7), date, 120) <= '2020-01' 

Union

SELECT   SUM(Price)                    AS 'Total'
         ,CONVERT(char(7), date, 120)  AS 'year'
		 ,Department as 'Grocery'
FROM     [dbo].[Expenses]
WHERE    Department=( 'Grocery')
GROUP BY  Rollup (CONVERT(char(7), date, 120)), Department  
HAVING   CONVERT(char(7), date, 120) >= '2017-01'  
AND      CONVERT(char(7), date, 120) <= '2020-01' 


But like this i could not use it for my Charts in Vb.net.
Is it possible to do it differently?

What I have tried:

This is what I have tried, it doesn't give me any error but also it won't give me any data


SELECT   SUM(Price)                    AS 'Total'
         ,CONVERT(char(7), date, 120)  AS 'year'
		 ,Department as 'Department'
FROM     [dbo].[Expenses]
WHERE    Department=('Electronics,Grocery')
GROUP BY  Rollup (CONVERT(char(7), date, 120)), Department  
HAVING   CONVERT(char(7), date, 120) >= '2017-01'  
AND      CONVERT(char(7), date, 120) <= '2020-01' 


But I have no results.
Thank you for your help.
Posted
Updated 1-Aug-20 15:40pm

1 solution

If you want to display both departments replace this line:
SQL
WHERE    Department=('Electronics,Grocery')

with:
SQL
WHERE    Department IN ('Electronics', 'Grocery')


For further details, please see: SQL: IN Condition[^]

If you want to display all departments, remove or comment WHERE statement (use double '-'):
SQL
-- WHERE Department IN ('Electronics', 'Grocery')
 
Share this answer
 
v2
Comments
Member 13410460 17-Jul-20 7:20am    
Thank you mate, that worked perfectly fine.
Maciej Los 17-Jul-20 7:36am    
You're very welcome.

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



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