I have 2 tables:
Table1
________
Id
Name
Desc
Notes
Table2
_______
Id
Table1ID
Total
Avail
Details
Misc
Table2 can contain multiple records for each Name from Table1.
I need to retrieve the count of all recods in Table1 where sum of Avail is less than 10% of sum of the Total in Table2
I have the following query:
SELECT COUNT(a.Id)
FROM Table1 a
LEFT OUTER JOIN Table2 b ON a.Id = b.Table1ID
WHERE not b.Total is null
AND SUM(b.Avail) < (SUM(b.Total)/10)
but it gives me an error "An aggregate may not appear in the WHERE clause..."
I tried
SELECT COUNT(a.Id)
FROM Table1 a
LEFT OUTER JOIN Table2 b ON a.Id = b.Table1ID
WHERE not b.Total is null
HAVING SUM(b.Avail) < (SUM(b.Total)/10)
but also got an error about Arithmetic overflow
How can I accomplish my task in one query?
Start Free Trial