简体   繁体   English

T-SQL获取唯一键列的所有记录,该唯一键列包含另一个列值

[英]T-SQL Get all records of a unique key column that contains a column value both not another

Hope I can describe it well. 希望我能很好地描述它。 I have a table called TruckAuftragworkflow where I have a column called TruckAppId and a column called TaskId . 我有一个名为TruckAuftragworkflow的表,其中有一个名为TruckAppId的列和一个名为TaskId的列。 I want to get all TruckAppId from that table where the TaskId!=35 but TaskId=31 OR TaskId=32. 我想从该表中获取所有TruckAppId ,其中TaskId!= 35但TaskId = 31 OR TaskId = 32。

I tried that and failed: 我尝试过但失败了:

select count(*) AnzahlTasks, TruckAppId from TruckAuftragWorkFlow
GROUP BY TruckAppId
HAVING TaskId != 35 and TaskId in (31,32)

Maybe I have a knot in my head but it seems not that easy to build such query (for me!). 也许我的脑袋打了个结,但建立这样的查询似乎并不容易(对我来说!)。

在此处输入图片说明

Count your conditions with an aggregate function 用汇总函数计算条件

select count(*) AnzahlTasks, TruckAppId from TruckAuftragWorkFlow
GROUP BY TruckAppId
HAVING SUM(CASE WHEN TaskId = 35 THEN 1 ELSE 0 END) = 0 
   AND SUM(CASE WHEN TaskId in (31,32) THEN 1 ELSE 0 END) > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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