Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Query - Multiple Entries

  • 30-11-2018 9:48am
    #1
    Registered Users, Registered Users 2 Posts: 1,799 ✭✭✭


    Hello,

    I would appreciate some help on the following SQL query:-

    I have a table - Employees

    Name || Office
    John || Dublin
    Mary || Cork
    Wayne || Kerry
    Sean || Dublin
    John || Louth
    Mary || Dublin

    How would you determine which employees have more than 1 office?


Comments

  • Registered Users, Registered Users 2 Posts: 4,700 ✭✭✭sheroman01


    I'll give you a hint...Select your names, with a count(office) and then you need to GROUP ;)


  • Registered Users, Registered Users 2 Posts: 17,473 ✭✭✭✭Blazer


    Diceicle wrote: »
    Hello,

    I would appreciate some help on the following SQL query:-

    I have a table - Employees

    Name || Office
    John || Dublin
    Mary || Cork
    Wayne || Kerry
    Sean || Dublin
    John || Louth
    Mary || Dublin

    How would you determine which employees have more than 1 office?

    Use count.
    This is basic sql stuff. A quick google should show you the exact query to use.
    More complicated scripts will only return the names of employees with more than one office.


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    If you only want to return those with more than 1 office, then you'd need a HAVING in there too.

    SELECT
    FROM
    GROUP BY
    HAVING

    Fill in the blanks...


  • Registered Users, Registered Users 2 Posts: 247 ✭✭Tweeter


    You have a database design problem which you should address first.
    You have duplicate employees and duplicate locations.
    You need to create 3 tables to avoid this, an employee table, a location table and an assignment table


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    Tweeter wrote: »
    You have a database design problem which you should address first.
    You have duplicate employees and duplicate locations.
    You need to create 3 tables to avoid this, an employee table, a location table and an assignment table
    Okay, maybe getting a little ahead of yourself.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,799 ✭✭✭Diceicle


    Thanks for all the replies.
    They have been very helpful and have put me on the right path.

    If I wanted to output the 'duplicates' but in a more tabular style format

    Name Office1 Office2
    John Dublin Louth
    Mary Cork Dublin

    Would setting up cases be the optimum solution do you think.


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    That's a bit more complicated. You are looking at using PIVOT


  • Registered Users, Registered Users 2 Posts: 7,836 ✭✭✭Brussels Sprout


    Diceicle wrote: »
    If I wanted to output the 'duplicates' but in a more tabular style format

    Name Office1 Office2
    John Dublin Louth
    Mary Cork Dublin

    Would setting up cases be the optimum solution do you think.

    Join the table to itself with the join being on the basis of equal names but not equal offices. This is assuming that the Employees table only has the two columns mentioned above (joining on non-numeric values is kinda gross).


  • Closed Accounts Posts: 9,046 ✭✭✭Berserker


    Tweeter wrote: »
    You have a database design problem which you should address first.

    Doubt he or she designed it.


  • Registered Users, Registered Users 2 Posts: 22,639 ✭✭✭✭ELM327


    Join the table to itself with the join being on the basis of equal names but not equal offices. This is assuming that the Employees table only has the two columns mentioned above (joining on non-numeric values is kinda gross).
    That's a very messy join, as a rule you should only join on unique numeric ID imo.


  • Advertisement
Advertisement