SUM Question

Database

SUM Question

Here's another question which I'm sure is an easy one...

Let's say I have a table with the following data...

A 2
A 3
A 1
A 6
B 3
B -3

If I wanted to sum up and group them by the first column, I'd get...

A 12
B 0

Well, what if I didn't want to have the B shown if the SUM was 0. The error I'm getting now is "Improper use of an aggregate function in a WHERE clause", but I'm unsure how to get it in there.

I have this under my SELECT statement: SUM(Chrge_Amt) AS Total

And I've tried both of the following under my WHERE statement:
SUM(Chrge_Amt) <> 0
Total <> 0

How can I accomplish this?

2 REPLIES
rgs
Enthusiast

Re: SUM Question

Try a the HAVING clause

SELECT gr, SUM(chrg_amt)
GROUP BY gr
HAVING SUM(chrg_amt) <> 0
ORDER BY gr;
Enthusiast

Re: SUM Question

For aggregate functions we have to use having for any relational operation.

try with having inspite of where