|
-
Jan 7th, 2008, 09:29 AM
#1
Thread Starter
Addicted Member
[RESOLVED] help with sql: count
Hello,
I have a table which apart from other columns, has id and rid columns. id is the primary key and rid is a reference to another table.
What I want to do is retrieve the rid's which have more than one row in the table.
I tried this
Code:
SELECT * FROM address where count(rid)>1;
which throws up an error.
I tried something with joins which returned all the rows.
What is the correct way to achieve what I want to?
Thank you.
-
Jan 7th, 2008, 09:36 AM
#2
Re: help with sql: count
When using an aggregate in the filtering of the SQL, it can't be in the WHERE clause. It goes in the HAVING clause instead. The reason for this is because the WHERE will be executed first to filter out the data, then the aggregates are performed, THEN the HAVING clause it executed.
Code:
SELECT Rid FROM Address HAVING COUNT(Rid) > 1
-tg
-
Jan 7th, 2008, 10:50 AM
#3
Thread Starter
Addicted Member
Re: help with sql: count
Thank you. But that is returning only one value, while there are several rid's whose count is greater than 1.
-
Jan 7th, 2008, 10:54 AM
#4
Re: help with sql: count
Try this:
SELECT RID, COUNT(RID) FROM Address GROUP BY RID
See how many RIDs really are duplicated.
-tg
-
Jan 7th, 2008, 10:55 AM
#5
Re: help with sql: count
SELECT Rid, Count(*)
FROM Address
Group By Rid
HAVING COUNT(*) > 1
-
Jan 7th, 2008, 11:02 AM
#6
Thread Starter
Addicted Member
Re: help with sql: count
Thanks brucevde. Your query was spot on. Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|