Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two tables need help limiting by SUM 1

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
SI
Hello,

I've got two tables:

users
---------
id | name
---------
1 | John
2 | Bob

id ... unique ID number of the user
name ... Name of the user

and

sales
-----------------
id | user| amount
-----------------
1 | 1 | 10.00
2 | 2 | 13.00
3 | 1 | 9.40
4 | 1 | 20.15

id ... unique sale ID number
user ... user ID, same as "id" in "users" table
amount ... total sale amount


Now, I can get the total sales amount for each user like this:

SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
GROUP BY u.id
LIMIT 0 , 30

This returns

id | total
----------
1 | 43.15
2 | 9.40

which is correct.

But what I need to do is return only those users, who have total in sales at least 10.00. I tried:

SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
AND total >= 10.00
GROUP BY u.id
LIMIT 0 , 30

but I get error "unknow colum total". I can't figure out a way to do it. Any suggestions how to handle this?

Thanks in advance!
 
Code:
SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
GROUP BY u.id
having sum(s.amount) > 10.00
order by total desc
LIMIT 0 , 30
 
You're the man, thanks a ton!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top