So I have a query that does multiple sums that works great. Here is a stripped down version.
select sum(Case when PLU='WIDG101' then qty else 0 end) as Widget1,
sum(Case when PLU='WIDG201' then qty else 0 end) as Widget2
from SalesTable
But I have a specific list of PLUs that I want to use and may need to change from time to time. So I declared some tables and populated it like this:
Declare @WidgetTable1 (Widget1s varchar(20))
Declare @WidgetTable2 (Widget2s varchar(20))
Insert into WidgetTable1 Values ('WIDG101'),('WIDG102'),('WIDG103')
Insert into WidgetTable2 Values ('WIDG201'),('WIDG202'),('WIDG203')
select sum(Case when PLU IN (Select * from @WidgetTable1) then qty else 0 end) as Widget1,
sum(Case when PLU IN (Select * from @WidgetTable2) then qty else 0 end) as Widget2
from SalesTable
But I get: Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Which I understand but I am stuck on a way around it.
Any Suggestions?