简体   繁体   English

根据SQL Server中的条件从表中检索列值

[英]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为同一JobNoStatus =“交付”。

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)输出,而ProgramIdStatus为“已传递”。

点击这里查看表格

Your question suggests group by with having : 你的问题建议group byhaving

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.

相关问题 无法根据 SQL 服务器中另一列的条件从列中检索不同的行 - Could not able to retrieve distinct rows from a Column based on the condition of another column in SQL server 从SQL Server表中检索多个值 - Retrieve multiple values from SQL Server table 从XML数据列中将SQL Server中的多个值检索(分解)到表中的同一单元格(行/列交集)中 - Retrieve (Shredding) Multiple Values in SQL Server into same cell (row/ column intersection) in table from XML data column 如何基于SQL Server中当前表列的值使用与其他表不同的列值 - How to use different column values from other table based on value on current table column in SQL server 根据条件 SQL 服务器更新列中的一些值 - Update some values in a column based on a condition SQL Server SQL Server中基于第一个表列值的条件为null - Condition in SQL server based on first table column value is null 如何根据条件向现有的 SQL 服务器表添加列? - How to add column to an existing SQL Server table based on condition? 如何根据条件更新SQL SERVER DB中的表列 - How to Update table column in SQL SERVER DB based on condition 如何根据SQL Server中的条件从另一个表添加新列 - How to add new column from another table based on a condition in sql server SQL根据列条件检索行 - SQL retrieve rows based on column condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM