patternMinor
Only show results greater than a certain number when summing two values together
Viewed 0 times
shownumbergreatersummingthantogethertwowhenvaluesresults
Problem
I have a query that sums up two values in the same table and then calculates one as a percentage of the other
What I want to do is only return results where that value is greater than a certain amount (say 4 in this case). If I add something in the where clause such as:
I get the error:
An aggregate may not appear in the WHERE clause...
I expect there's an easy way round this that I haven't learned yet. Any ideas?
select account_no,
100*((sum(COMMISSION/100)) / (SUM(AMOUNT/100)))
from accounts etcWhat I want to do is only return results where that value is greater than a certain amount (say 4 in this case). If I add something in the where clause such as:
where 100*((sum(COMMISSION/100)) / (SUM(AMOUNT/100))) > 4I get the error:
An aggregate may not appear in the WHERE clause...
I expect there's an easy way round this that I haven't learned yet. Any ideas?
Solution
Try:
However you will still probably find that integer math is preventing you from getting the result you want. So this might be better:
HAVING 100*((sum(COMMISSION/100)) / (SUM(AMOUNT/100))) > 4However you will still probably find that integer math is preventing you from getting the result you want. So this might be better:
HAVING 100*((1.0*sum(COMMISSION/100)) / (1.0*SUM(AMOUNT/100))) > 4Code Snippets
HAVING 100*((sum(COMMISSION/100)) / (SUM(AMOUNT/100))) > 4HAVING 100*((1.0*sum(COMMISSION/100)) / (1.0*SUM(AMOUNT/100))) > 4Context
StackExchange Database Administrators Q#108305, answer score: 6
Revisions (0)
No revisions yet.