locked
SQL Query to pull records where there is more than one Department by User Id RRS feed

  • Question

  • User-1188570427 posted

    Hello,

    I am trying to find out all users that have 2 different departments in their profile in my web application.

    Basically we have a User Id and then Department column.  We have their main profile and then we have a history table where their values have changed as their accounts have been updated.  Is there a query out there that can give me all users where they have more than 1 different Department from the history table?

    Here is an example

    User ID = Department

    1 = SALES

    1 = IT

    1 = SUPPORT

    2 = SALES

    2 = SALES

    2 = SALES

    3 = IT

    3 = IT

    3 = SALES

    So the query should pull back user ID 1 and 3.  User ID 1 has Sales, IT,  and Support as their departments (they were moved a lot!).  User ID 3 has IT and Sales,

    Thanks.

    Friday, June 10, 2016 11:32 AM

Answers

  • User941753370 posted

    Hi,

    Try:

    select UserID
    from History
    group by UserID
    having count(distinct Department) > 1

    Hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2016 12:55 PM

All replies

  • User941753370 posted

    Hi,

    Try:

    select UserID
    from History
    group by UserID
    having count(distinct Department) > 1

    Hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2016 12:55 PM
  • User-1188570427 posted

    Hi,

    Try:

    select UserID
    from History
    group by UserID
    having count(distinct Department) > 1

    Hope this help

    This did the trick!  Thank you!

    Friday, June 10, 2016 1:15 PM