need help with an aggregate function in a SQL where clause

bstineman

Junior Member
Sep 11, 2002
2
0
0
Bear with me, I suck at doing subqueiries and need a bit of help.

I want to do:
Select SUM(mycount),item_id FROM mytable WHERE SUM(mycount)>10 AND DATEDIFF(day,datestamp,GETDATE())<=30 GROUP BY item_id

This of course fails with an error like the follow:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

What's the optimal way do to this?
 

isasir

Diamond Member
Aug 8, 2000
8,609
0
0
Does this work?

Select SUM(mycount),item_id
FROM mytable
WHERE DATEDIFF(day,datestamp,GETDATE())<=30
GROUP BY item_id
HAVING SUM(mycount)>10;