在单个(大型)SQL 查询中使用 CASE 语句作为 COUNT 函数的前身,语法

发布于 2024-11-13 08:13:35 字数 1429 浏览 2 评论 0原文

我在将一些逻辑合并到大型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-11-20 08:13:35

我没有尝试解开您的整个语句,但听起来您想像这样使用 CASE 语句:

SUM(CASE WHEN BIAdmin.Item.ItemStatus = 'inactive' THEN 0 ELSE 1 END) As ActiveItemCount

该语句将出现在您的 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:

SUM(CASE WHEN BIAdmin.Item.ItemStatus = 'inactive' THEN 0 ELSE 1 END) As ActiveItemCount

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.

穿越时光隧道 2024-11-20 08:13:35

您的 CASE 语句没有 END,并且您的最后一个 SELECT 在该上下文中无效。尝试这样的事情:

SELECT 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,
    sum(case when BIAdmin.Item.ItemStatus != 'inactive' then 1 end) as NotInactiveCount,
    count(*) as TotalCount
FROM BIAdmin.Quote 
LEFT OUTER JOIN BIAdmin.Item ON BIAdmin.Quote.ID = BIAdmin.Item.QuoteID
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]'))

There is no END for your CASE statement, and your last SELECT is not valid in that context. Try something like this:

SELECT 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,
    sum(case when BIAdmin.Item.ItemStatus != 'inactive' then 1 end) as NotInactiveCount,
    count(*) as TotalCount
FROM BIAdmin.Quote 
LEFT OUTER JOIN BIAdmin.Item ON BIAdmin.Quote.ID = BIAdmin.Item.QuoteID
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]'))
旧伤慢歌 2024-11-20 08:13:35
SELECT DISTINCT  BIAdmin.Item.ID,
    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,
    SUM( ( case when BIAdmin.Item.ItemStatus = 'inactive' THEN 1 ELSE 0 END))
FROM BIAdmin.Quote LEFT OUTER JOIN BIAdmin.Item ON BIAdmin.Quote.ID = BIAdmin.Item.QuoteID

GROUP BY BIAdmin.Item.ID, 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]'))
SELECT DISTINCT  BIAdmin.Item.ID,
    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,
    SUM( ( case when BIAdmin.Item.ItemStatus = 'inactive' THEN 1 ELSE 0 END))
FROM BIAdmin.Quote LEFT OUTER JOIN BIAdmin.Item ON BIAdmin.Quote.ID = BIAdmin.Item.QuoteID

GROUP BY BIAdmin.Item.ID, 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]'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文