具有多个HAVING条件的SQL Server查询

时间:2017-03-28 17:45:13

标签: ssms having

我们说我们有Table1

ID Month Code
----------
1 Dec 

1 Jan Yes

2 Dec

2 Jan 

如何使用(ID'sDec)选择Jan并遗漏Yes代码?我希望我的查询返回ID 2而不是ID 1。我知道我在AND条款中不能有HAVING

1 个答案:

答案 0 :(得分:0)

尝试此查询

SELECT ID FROM Table1 WHERE Month = "Dec" AND Code <> "yes" AND ID IN (SELECT ID FROM Table1 WHERE Month = "Jan" AND Code <> "yes")