不同查询之间的SQL不同值

时间:2013-09-24 07:34:16

标签: sql duplicate-removal

我有以下查询

select distinct 
people.id
from people
inner join incident on people.id = incident.peopleid 
where  incident.service =4
AND people.id NOT IN (
select  incident.peopleid
from Incident
where 
incident.service IN =4
AND incident.ServiceInterrupt != 0
)

这给了我所有属于服务的人,他们还没有中断该服务。 我想对服务5和6运行此查询,并且这些结果仅采用唯一的人。  直到现在,我运行查询3次(每次更改服务)然后我删除重复与excel! 还有其他办法吗?

提前致谢

编辑:更具体地说,我希望从结果中排除服务X中仅具有中断服务X的所有人员。这就是为什么我不能在(4,5,6)中使用incident.service来代替incident.service = 4

2 个答案:

答案 0 :(得分:2)

我认为你可以使用这样的东西;

select distinct 
people.id
from people
inner join incident i1 on people.id = i1.peopleid 
where  i1.service in(4,5,6)
AND people.id NOT IN (
select  i2.peopleid
from Incident i2
where 
i2.service = i1.service
AND i2.ServiceInterrupt != 0
)

答案 1 :(得分:0)

试试这个:

select distinct 
people.id
from people
inner join incident on people.id = incident.peopleid 
where  incident.service IN(4,5,6)
AND incident.ServiceInterrupt != 0