我有来自表
的以下样本数据Id Dsc
500001 INSURED
500001 THIRD PARTY
500001 THIRD PARTY
500001 THIRD PARTY
500002 INSURED
500002 THIRD PARTY
500003 INSURED
500004 BROKER
500005 CLAIMANT
我希望提取那些Dsc既不是“被保险人”的ID。也不是经纪人。由于两列都有重复数据,因此我设计了以下查询..
Select Id from table1 where Dsc not in ('Insured', 'Broker')
Except
Select Id from table1 where Dsc in ('Insured', 'Broker')
有没有其他方法可以做到这一点?
答案 0 :(得分:6)
SELECT id FROM table1 GROUP BY id
HAVING SUM(CASE WHEN Dsc='Insured' THEN 1 ELSE 0 END)=0
AND SUM(CASE WHEN Dsc='Broker' THEN 1 ELSE 0 END)=0
答案 1 :(得分:1)
您可以将查询编写为:
SELECT Id FROM (
SELECT Id , SUM(CASE WHEN Dsc IN ('INSURED','BROKER') THEN 1 ELSE 0 END ) AS Condition
FROM @Test
GROUP BY Id
) T
WHERE Condition = 0
答案 2 :(得分:0)
使用子查询:
Select id,dsc from (select id,dsc from table1 group by id, dsc)
where dsc not in('Insured', 'Broker')
答案 3 :(得分:-1)
使用此代码停止重复列
Select distinct *from table1