Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL Select Problem

  • 28-08-2007 11:47AM
    #1
    Registered Users, Registered Users 2 Posts: 10,148 ✭✭✭✭


    Hoping someone can help me out here . . .

    I have a table that contains a number of records. It has two fields, "Starter" and "Main Course". Typical data would look like this. . .

    Starter¦Main
    Soup, Beef
    Soup, Chicken
    Salad, Pork
    Soup, Beef
    Salad, Chicken
    Soup, Beef
    Salad, Pork
    etc . . .

    I need to try and select all similar pairs that occur in the table more than once. For example, in the above table, I would be looking to select Soup and Beef and Salad and Pork as they occur more than twice.

    Anyone help?


Comments

  • Registered Users, Registered Users 2 Posts: 273 ✭✭stipey


    My first tip would be to search the web - because it certainly doesn't strike me as a query somebody would still need assistance with after a quick trawl through google.

    The answer is more likely to stick in your mind if you discover it yourself through google or MSDN.

    I'll point you in the direction however - the key words to search for are "count", "group by" and (if you want to find instances where a combination appears more than once "having". It would help if you were to add +sql to your search to ensure you only get hits relating to SQL.


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    SELECT DISTINCT Starter, Main WHERE Count(Starter + Main) > 1


  • Registered Users, Registered Users 2 Posts: 5,103 ✭✭✭mathie


    SELECT starter, main, COUNT(*) AS Expr1
    FROM yourtable
    GROUP BY starter, main
    HAVING (COUNT(*) > 1)


  • Registered Users, Registered Users 2 Posts: 10,148 ✭✭✭✭Raskolnikov


    That works a peach Mathie, the problem I was having was that I was trying to put the (COUNT(*) > 1) into the WHERE part of my SQL query, that of course gave me an error.


Advertisement