SQL SELECT 使用 CASE 语句,但有多个条件

发布于 2024-10-28 13:16:01 字数 724 浏览 1 评论 0原文

我正在尝试使用 CASE 语句执行 SQL SELECT 查询,该查询运行率为 100%。我的代码如下所示:

SELECT t.TASK_WINDOW, 
   SUM(CASE WHEN t.TASK_NAME = 'DEV' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS DevComplete,
   SUM(CASE WHEN t.TASK_NAME = 'TEST' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS TestComplete,
   SUM(CASE WHEN t.TASK_NAME = 'RELEASE' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS ReleaseComplete
FROM Requirements r
    INNER JOIN Tasks t
        ON r.TASK = t.REQ_ID
GROUP BY t.TASK_WINDOW

但是我的问题是我对所有三个 SUMS 有一个附加标准。复杂性在于,一个需求可能具有每种类型的多个任务,但例如,只有当一个需求的所有开发任务都完成时,该需求才必须是开发任务完成的,否则它是不完整的。

我真的希望能够在所有开发任务完成、所有测试任务完成以及所有发布任务完成的情况下测量需求量,但将它们全部分组到最新的开发任务强制窗口中。

请帮忙;-)

I'm trying to perform a SQL SELECT query using a CASE statement, which is working 100%. My code looks as follows:

SELECT t.TASK_WINDOW, 
   SUM(CASE WHEN t.TASK_NAME = 'DEV' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS DevComplete,
   SUM(CASE WHEN t.TASK_NAME = 'TEST' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS TestComplete,
   SUM(CASE WHEN t.TASK_NAME = 'RELEASE' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) AS ReleaseComplete
FROM Requirements r
    INNER JOIN Tasks t
        ON r.TASK = t.REQ_ID
GROUP BY t.TASK_WINDOW

However my problem is that I have an additional criteria for all three SUMS. The complexity is that a requirement might have multiple tasks of each type, but for example an requirement must only be Dev Task complete if all its Dev Tasks are complete, otherwise it is incomplete.

I really want to be able to measure the amount of requirements with all its dev tasks complete, and also all its test tasks complete and all its release tasks complete, but group them all against the latest Dev Task compelted's window.

Please help ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴我心暖 2024-11-04 13:16:01

您需要将总数与完整的数字进行比较:

SELECT t.TASK_WINDOW,     

CASE
WHEN SUM(CASE WHEN t.TASK_NAME = 'DEV' THEN 1 ELSE 0 END) =
   SUM(CASE WHEN t.TASK_NAME = 'DEV' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) 
Then 1 
ELSE 0 
END as AllDevComplete

FROM Requirements r
  INNER JOIN Tasks t
      ON r.TASK = t.REQ_ID
GROUP BY  t.TASK_WINDOW  

或者您可以查找不完整的数字。但同样的基本技巧——一个案例的总和——将会起作用。

You need to compare the total with the number complete:

SELECT t.TASK_WINDOW,     

CASE
WHEN SUM(CASE WHEN t.TASK_NAME = 'DEV' THEN 1 ELSE 0 END) =
   SUM(CASE WHEN t.TASK_NAME = 'DEV' AND t.TASK_STATUS = 'Completed' THEN 1 ELSE 0 END) 
Then 1 
ELSE 0 
END as AllDevComplete

FROM Requirements r
  INNER JOIN Tasks t
      ON r.TASK = t.REQ_ID
GROUP BY  t.TASK_WINDOW  

Alternatively you could look for the number which are NOT complete. But the same basic trick - a case of a sum of a case - will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文