SQL SELECT 使用 CASE 语句,但有多个条件
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将总数与完整的数字进行比较:
或者您可以查找不完整的数字。但同样的基本技巧——一个案例的总和——将会起作用。
You need to compare the total with the number complete:
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.