johnson1
asked on
aggregate only get when >0
Hello,
How do I only to get records that have Cnt>0 in
SELECT MyTableId, Cnt=(SELECT COUNT(*) FROM OtherTable o where o.MyTableId=m.MyTableId)
FROM MyTable m
How do I only to get records that have Cnt>0 in
SELECT MyTableId, Cnt=(SELECT COUNT(*) FROM OtherTable o where o.MyTableId=m.MyTableId)
FROM MyTable m
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT o.MyTableId, COUNT(*) as cnt
FROM OtherTable o
GROUP BY o.MyTableId
HAVING COUNT(*) > 0
Use a HAVING clause which allows you to filter the aggregated values, here it will remove all counts of zero from the result.