简体   繁体   中英

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

I need to retrieve only those JobNo where all the ProgramId for the same JobNo have Status = "delivered".

From the given image only JobNo (1&4) should be output as only JobNo (1&4) the ProgramId 's Status ="delivered".

点击这里查看表格

Your question suggests group by with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM