Subquery count being doubled

Hi all.

I have the following query that contains a subquery. I want to only display records whose count (from the subquery) is greater than 1.

The problem is that the count it's displaying is twice as many as the original count. So for example, it's showing record ABC as having a count of 4, when in reality the count is 2. When I use the subquery (SQ) as it's own query it correctly shows a count of 2, but when I add it to the query below (as a subquery) it doubles the count. What am I doing wrong?

SELECT DISTINCT a.GPOContractID, b.EntityCode, COUNT(SQ.EntityCode) AS Count
FROM         ContractAssignmentHeader AS a INNER JOIN
                      ContractAssignmentDetail AS b ON a.CANumber = b.CANumber INNER JOIN
                          (SELECT DISTINCT ContractAssignmentHeader.CANumber, ContractAssignmentHeader.GPOContractID, ContractAssignmentDetail.EntityCode
                            FROM          ContractAssignmentHeader INNER JOIN
                                                   ContractAssignmentDetail ON ContractAssignmentHeader.CANumber = ContractAssignmentDetail.CANumber
                            WHERE      (ContractAssignmentDetail.Status = N'Active') AND (ContractAssignmentHeader.ContractType = N'GPO')) AS SQ ON 
                      a.GPOContractID = SQ.GPOContractID AND b.EntityCode = SQ.EntityCode
GROUP BY a.GPOContractID, b.EntityCode
HAVING     (COUNT(SQ.EntityCode) > 1)
ORDER BY a.GPOContractID

Open in new window


Thank you in advance.
printmediaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Well now, the subquery seems to be repeating the main query, and joining back on
SQ ON a.GPOContractID = SQ.GPOContractID AND b.EntityCode = SQ.EntityCode

Open in new window


So that means you are going to get double the rows - from the main datasource and then the subquery.

They all use inner joins, so it could also be expressed (using table alias) as
SELECT DISTINCT a.GPOContractID, b.EntityCode, COUNT(SQ.EntityCode) AS Count

FROM         ContractAssignmentHeader AS a 
INNER JOIN   ContractAssignmentDetail AS b ON a.CANumber = b.CANumber 
INNER JOIN   ContractAssignmentHeader as C on a.GPOContractID = C.GPOContractID and C.ContractType = N'GPO'
INNER JOIN   ContractAssignmentDetail as D ON C.CANumber = D.CANumber  AND b.EntityCode = d.EntityCode and D.Status = N'Active' 

GROUP BY a.GPOContractID, b.EntityCode
HAVING     (COUNT(SQ.EntityCode) > 1)
ORDER BY a.GPOContractID

Open in new window

When you look at that, you notice the joins with more clarity and realise that mayb the alias C and D arent really needed, because they are basically filters on alias a and b (respectively). So, it can be further simplified....
SELECT DISTINCT a.GPOContractID, b.EntityCode, COUNT(SQ.EntityCode) AS Count

FROM        ContractAssignmentHeader AS a 
INNER JOIN  ContractAssignmentDetail AS b ON a.CANumber = b.CANumber 

WHERE      (b.Status = N'Active') 
AND        (a.ContractType = N'GPO')

GROUP BY    a.GPOContractID, b.EntityCode
HAVING     (COUNT(SQ.EntityCode) > 1)
ORDER BY    a.GPOContractID

Open in new window

And when you look at that, it is pretty close to just the subquery. And because you are doing GROUP BY, you probably dont need the DISTINCT....

Anyway, have a look at the above and see if it works for you, and if it makes sense...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
printmediaAuthor Commented:
Thank you for explaining it in detail! Removing the DISTINCT made the query even faster. Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.