I’m having trouble incorporating a bit of logic into a large SQL query. I’m using SQL Server Reporting Services 2005 with the Report Designer, and it only gives you one area to define a single SQL query to populate the report with. Hopefully someone can tell me what’s wrong with my syntax so I can get it running.
I need to not only select a bunch of attributes filtering by (QuoteOrderStatus != ‘Closed’), but also to only count the quotes with (ItemStatus != ‘inactive’) in a COUNT(DISTINCT ItemID’s) statement (an aggregate function). I don’t want to simply filter out the quotes with inactive items, because I still want to display the quotes with zero items. I only want to not include them in the total itemCount.
I did research and it seems the way to accomplish this type of logic is using CASE statements, but I’m having trouble incorporating them into this one obfuscated query. Report Designer simply tells me it’s wrong and rearranges it to a way that I want even less. Can anyone help me with fixing this statement so it would be accepted as a valid query?
I’ve researched this for hours now without finding a good resource for this specific kind of problem. Sorry if the code is unclear, I tried to explain this as well as I can. Please ask questions if you want me to clarify in some way, and I appreciate any help anyone can offer.
SELECT DISTINCT
BIAdmin.Quote.QuoteOrderSalesOffice, BIAdmin.Quote.ManufacturerQuoteNumber,
BIAdmin.Quote.CustomerName, BIAdmin.Quote.Project_JobTitle,
BIAdmin.Quote.EndUserName, BIAdmin.Quote.QuoteOrderStatus,
SUM(BIAdmin.Item.TotalPriceOfItemNumber) AS totalValue, BIAdmin.Item.ItemStatus
FROM BIAdmin.Quote LEFT OUTER JOIN BIAdmin.Item ON BIAdmin.Quote.ID = BIAdmin.Item.QuoteID
WHERE BIAdmin.Item.ItemStatus LIKE
CASE WHEN NOT (BIAdmin.Item.ItemStatus = 'inactive') THEN
SELECT DISTINCT COUNT(BIAdmin.Item.ID) AS itemCount
GROUP BY BIAdmin.Quote.QuoteOrderSalesOffice, BIAdmin.Quote.ManufacturerQuoteNumber,
BIAdmin.Quote.CustomerName, BIAdmin.Quote.Project_JobTitle,
BIAdmin.Quote.EndUserName, BIAdmin.Quote.QuoteOrderStatus, BIAdmin.Item.ItemStatus
HAVING (NOT (BIAdmin.Quote.QuoteOrderStatus = N'[Closed]'))
I haven’t tried to unravel your whole statement, but it sounds like you want to use your CASE statement like this:
This statement would go in your select clause rather than in your where clause. Using “Like” the way you isn’t going to work. Try taking it out altogether (and try it once without DISTINCT) and adding the sum above to see what your results look like.