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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Count and Group Problem...

Status
Not open for further replies.

Akumadevil

Programmer
Sep 25, 2005
9
AU
Hi,
I have the following 2 tables:

-------------------------------

users (user_id, name)
sessions (user_id, paid_date)

-------------------------------

I want a query to show all users names who have more than 5 (paid_date != null)

So far I can group the sessions where paid_date != null which might give the following:

user_id, count(*)
1 7
2 10
3 2

But when I try my entire query:

select sessions.user_id, count(*) as abc
from sessions, users
where sessions.paid_date != 'null' and abc > 5 and sessions.user_id = users.user_id
group by sessions.user_id

It stuffs up on the abc > 5 part.

Any ideas?
 
To test the result of an aggregate function you must use the HAVING clause:
...
GROUP BY sessions.user_id
HAVING COUNT(*)>5

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top