Question : Count Column

Experts,

Why is the "GapCount" field below considered "invalid" in the HAVING clause?  I'm trying to select only those records where this value = 1.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
SELECT ogr.MemberID
,pt.PopulationTaskID
,COUNT(pt.PopulationTaskID) As GapCount
FROM OutcomeGapResult ogr
INNER JOIN PopulationTask pt ON pt.PopulationTaskID=ogr.PopulationTaskID 
LEFT OUTER JOIN [Population] p ON p.MemberID=ogr.MemberID 
WHERE pt.Active=1 
GROUP BY ogr.MemberID,pt.PopulationTaskID 
HAVING GapCount=1
ORDER BY ogr.MemberID

Answer : Count Column

SELECT ogr.MemberID
,pt.PopulationTaskID
,COUNT(pt.PopulationTaskID) As GapCount
FROM OutcomeGapResult ogr
INNER JOIN PopulationTask pt ON pt.PopulationTaskID=ogr.PopulationTaskID
LEFT OUTER JOIN [Population] p ON p.MemberID=ogr.MemberID
WHERE pt.Active=1
GROUP BY ogr.MemberID,pt.PopulationTaskID
HAVING COUNT(pt.PopulationTaskID)  =1
ORDER BY ogr.MemberID
Random Solutions  
 
programming4us programming4us