Two tables:
a) svn3 with columns name revno compname
b) archdetails with columns name version compname inFlat
Shouldn’t these 2 queries output the same result. The output of query 2 is correct. What is wrong with query1?
query1:
select sum(CASE WHEN inFlat=1 THEN 1 ELSE 0 END) from archdetails inner join svn3 on archdetails.compname=svn3.compname where archdetails.name='ant' AND version='1.4' AND (revno='r274642' OR revno='r274578' OR revno='r274533')
query2:
select sum(CASE WHEN inFlat=1 THEN 1 ELSE 0 END) from archdetails where name='ant' AND version='1.4' AND compname IN (select compname from svn3 where revno ='r274642' OR revno='r274578' OR revno='r274533')
If
svn3has more than one row with the samecompnamethat also have one of the requestedrevnovalues, then query 1 will count eachsvn3row once. Query 2 will only count eachcompnameonce. TheINclause is eliminating any duplicatecompnameentries in the second query.If you don’t want to count duplicate
compnameentries, then you need to group them in a subquery. One way to do that is using theINclause like you did in query 2. Another option would be to useGROUP BYon a subquery, and name that subquery. Then join the named subquery to thearchdetailstable.If you’re trying to solve a performance problem, then I would suggest adding primary keys and foreign keys to the tables. I think that would speed things up.
I’ve seen some comments that
EXISTSis sometimes faster thanIN, but I haven’t seen any evidence to back that up. You might want to experiment with that.