Results 1 to 6 of 6

Thread: [RESOLVED] help with sql: count

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Resolved [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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: help with sql: count

    Try this:

    SELECT RID, COUNT(RID) FROM Address GROUP BY RID

    See how many RIDs really are duplicated.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: help with sql: count

    SELECT Rid, Count(*)
    FROM Address
    Group By Rid
    HAVING COUNT(*) > 1

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    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
  •  



Click Here to Expand Forum to Full Width