在单个(大型)SQL 查询中使用 CASE 语句作为 COUNT 函数的前身,语法
我在将一些逻辑合并到大型 SQL 查询中时遇到困难。我将 SQL Server Reporting Services 2005 与报表设计器一起使用,它只为您提供一个区域来定义单个 SQL 查询来填充报表。希望有人能告诉我我的语法有什么问题,以便我可以让它运行。
我不仅需要选择一堆按 (QuoteOrderStatus != 'Closed') 过滤的属性,而且还只需要在 COUNT(DISTINCT ItemID's) 语句(聚合函数)中对 (ItemStatus != 'inactive') 的引号进行计数。我不想简单地过滤掉具有非活动项目的报价,因为我仍然想显示具有零项目的报价。我只想不将它们包含在总 itemCount 中。
我做了研究,似乎完成这种逻辑的方法是使用 CASE 语句,但我在将它们合并到这个混淆的查询中时遇到了麻烦。报表设计器只是告诉我这是错误的,并将其重新排列为我想要的更少。任何人都可以帮助我修复此语句,以便它被接受为有效查询吗?
我已经研究了几个小时,但没有找到解决此类特定问题的良好资源。抱歉,如果代码不清楚,我尽力解释这一点。如果您希望我以某种方式进行澄清,请提出问题,我感谢任何人可以提供的帮助。
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'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]'))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有尝试解开您的整个语句,但听起来您想像这样使用 CASE 语句:
该语句将出现在您的 select 子句中,而不是您的 where 子句中。以不适合工作的方式使用“喜欢”。尝试将其全部取出(并在没有 DISTINCT 的情况下尝试一次)并添加上面的总和,看看结果是什么样的。
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.
您的 CASE 语句没有 END,并且您的最后一个 SELECT 在该上下文中无效。尝试这样的事情:
There is no END for your CASE statement, and your last SELECT is not valid in that context. Try something like this: