Get records having both values in “IN” using SQL

夙愿已清 提交于 2019-12-14 03:08:05

问题


I need a SQL statement to extract those applicants from the database who have both the attachment tags. The belowq statement works fine if I want aaplicant who have either 7 or 8 as the attachment tag but i need applicant who have both these tags.

select distinct(a.id) from applicant a
join applicantdeployment ad on a.id = ad.applicantid
join ApplicantAttachment at on a.id = at.applicantid
where a.applicanttype = 'TCN/LN' and ad.groundlocation in (4,8,14) and ad.deploymentstatus =1
and ad.trainingdeploymentstatus = 6 and at.tag in (7,8)

For example from the below set I just want ids 7332,7451 and 7449 to show up.

ID      Tag
7328   8
7328   8
7332   8
7332   7
7337   7
7449   8
7449   7
7451   8
7453   7
7451   7

Thanks!


回答1:


You could join to the ApplicationAttachment table twice to ensure that both tags are present. That way you would avoid the need for DISTINCT and GROUP BY.

SELECT a.id
FROM applicant a
JOIN applicantdeployment ad ON a.id = ad.applicantid
JOIN ApplicantAttachment at7 ON a.id = at7.applicantid AND at7.tag = 7
JOIN ApplicantAttachment at8 ON a.id = at8.applicantid AND at8.tag = 8
WHERE a.applicanttype = 'TCN/LN' 
AND ad.groundlocation IN (4,8,14)
AND ad.deploymentstatus = 1
AND ad.trainingdeploymentstatus = 6



回答2:


You need to do below, rather than distinct:

GROUP BY a.id HAVING COUNT(a.id) = 2  



回答3:


In your sample data, shouldn't 7451 also be selected? You'll need to modify the code below to fit your query, but try out the following:

CREATE TABLE #Temp
(
    ID INT,
    Tag INT
)

INSERT INTO #Temp
(
    ID,
    Tag
)
SELECT 7328, 8 UNION
SELECT 7332, 8  UNION
SELECT 7332, 7  UNION
SELECT 7337, 7  UNION
SELECT 7449, 8  UNION
SELECT 7449, 7  UNION
SELECT 7451, 8  UNION
SELECT 7453, 7  UNION
SELECT 7451, 7

select distinct(ad.id) from #Temp ad
join #Temp at on ad.id = at.id
AND ad.Tag != at.Tag
where at.tag in (7,8)
AND ad.Tag IN (7, 8)



回答4:


If you put the IN list items into a table called #Tags, this will work, and there won't be any need to rewrite the query if the number of tags changes.

select distinct(#Temp.ID)
from #Temp
where not exists (
  select * from #Tags
  where not exists (
    select * from #Temp as TempCopy
    where TempCopy.Tag = #Tags.Tag
    and TempCopy.ID = #Temp.ID
  )
)  

In English, this selects those IDs for which none of the required tags is missing.




回答5:


Try:

select distinct(a.id)
from applicant a
join applicantdeployment ad on a.id = ad.applicantid
join (select applicantid from applicantattachment at1
join applicantattachment at2 on at1.applicantid=at.applicantid
where at1.tag=7 and at2.tag=8) at
on a.id = at.applicantid
where a.applicanttype = 'TCN/LN'
and ad.groundlocation in (4,8,14)
and ad.deploymentstatus =1
and ad.trainingdeploymentstatus = 6

Join the applicantattachment table to itself to get only the relevant applicantids.




回答6:


select distinct(a.id) from applicant a
join applicantdeployment ad on a.id = ad.applicantid
join ApplicantAttachment at on a.id = at.applicantid
where a.applicanttype = 'TCN/LN' and ad.groundlocation in (4,8,14) and ad.deploymentstatus =1
and ad.trainingdeploymentstatus = 6 and at.tag in (7,8) and a.id IN (7332,7449);


来源:https://stackoverflow.com/questions/10285761/get-records-having-both-values-in-in-using-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!