[英]Retrieve column values from table based on condition in SQL Server
I have a table with three columns: 我有一个包含三列的表:
(JobNo, ProgramId & Status)
(工作编号,程序编号和状态)
JobNo
have many ProgramId
and each ProgramId
has Status
JobNo
有许多ProgramId
,每个ProgramId
都有Status
I need to retrieve only those JobNo
where all the ProgramId
for the same JobNo
have Status
= "delivered". 我需要只检索
JobNo
,所有的ProgramId
为同一JobNo
有Status
=“交付”。
From the given image only JobNo
(1&4) should be output as only JobNo
(1&4) the ProgramId
's Status
="delivered". 从给定图像中,仅
JobNo
(1&4)应该作为JobNo
(1&4)输出,而ProgramId
的Status
为“已传递”。
Your question suggests group by
with having
: 你的问题建议
group by
与having
:
select jobid
from t
group by jobid
having min(status) = max(status) and min(status) = 'delivered';
Try something like this (not tested): 尝试这样的事情(未经测试):
SELECT JobNo
FROM MyTable
GROUP BY JobNo
HAVING SUM(IIF(Status = 'Delivered', 0 , 1)) = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.