Question

I have the following query

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
)

that gives me all the people that belong to a service and they havent interrupt that service yet. I want to run this query for service 5 and 6 and for these results take only the unique people. Till now, i run the query 3 times (changing the service each time) and then i remove the duplicates with excel!!! Is there an other way to do it?

thanks in advance

EDIT: To be more specific i want from the results to exclude all people in service X that have interrupt service X only. That is why i cant use incident.service in (4,5,6) instead on incident.service=4

Was it helpful?

Solution

i think you can use something like this;

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
)

OTHER TIPS

Try this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top