Link to home
Start Free TrialLog in
Avatar of johnson1
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
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure if it is deliberate or not but you have omitted GROUP BY in your question; but you refer to getting recordS (plural) so I assume a group by being used.

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.