Results 1 to 4 of 4

Thread: SQL - HAVING, brain not functioning

  1. #1
    Join Date
    Jan 2008
    Location
    Henners, happy Henners
    Beans
    41

    SQL - HAVING, brain not functioning

    Hello.. sorry, remedial question from brain malfunction.

    I have tbl.transactions with columns id, buyer, product. I need to find products that have been sold to a distinct buyer more than once. So if my data looked like this, my query should return 5 as that one's been purchased by buyer #1 more than once.

    (id, buyer, product)

    1 1 5
    2 1 5
    3 2 5
    4 2 6

    I'd be most grateful if someone can remind me how to walk. Thank you!

  2. #2
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: SQL - HAVING, brain not functioning

    From the top of my head (untested):

    Code:
    SELECT buyer, product, COUNT(id) AS num
    FROM transactions
    GROUP BY buyer, product
    HAVING COUNT(id) > 1
    Not even tinfoil can save us now...

  3. #3
    Join Date
    May 2006
    Location
    BH, Brazil
    Beans
    338

    Re: SQL - HAVING, brain not functioning

    Tested one:

    SELECT buyer, product, COUNT(product) AS total
    FROM transactions
    GROUP BY buyer, product
    HAVING total > 1;

    You will get a table with the specific buyer, specific product and the total number of this product.

  4. #4
    Join Date
    Jan 2008
    Location
    Henners, happy Henners
    Beans
    41

    Re: SQL - HAVING, brain not functioning

    Thank you both, saved me much brain ache.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •