使用自联接选择SQL Server
我有一个表,其中有如下记录:使用自联接选择SQL Server,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我有一个表,其中有如下记录: FieldId collationid Type Message --------------------------------------------- 1 1234 WC hello 2 1234 WR next message 3 1234 WZ again 4 1234 WX a
FieldId collationid Type Message
---------------------------------------------
1 1234 WC hello
2 1234 WR next message
3 1234 WZ again
4 1234 WX another message
5 ab12 WC this message
6 ab12 WR again
7 ab12 WZ misc message
8 5678 WC hello
9 5678 WR next message
10 5678 WZ again
11 5678 WX another message
当一个记录集有全部四条记录,即WC、WR、WZ和WX时,它就完成了。我需要一个sql来显示记录丢失的时间。在上一个表示例中,SQL将生成ab12,因为它只有WC、WR和WZ记录
感谢您能给我的任何帮助。使用COUNT()
和拥有:
SELECT collationid
FROM tbl
WHERE Type IN('WC', 'WR', 'WZ', 'WX')
GROUP BY collationid
HAVING COUNT(DISTINCT Type) < 4
选择collationid
来自tbl
其中键入('WC'、'WR'、'WZ'、'WX')
按排序规则分组
计数(不同类型)<4的
你的手指很快;-)这也是我建议的答案。向上投票。。。