SQL Query to return ID that have different deptID associated with it.

  • Table Data

    ---------------------------

    ID Dept-ID Version

    10 30 0

    20 30 0

    20 30 1

    20 40 2

    30 50 0

    30 50 1

    30 60 2

    40 30 0

    40 30 1

    -------------

    Expected Output

    ID

    20

    30

  • SELECT ID FROM dbo.Table GROUP BY ID HAVING COUNT(*) > 1

  • I guess this won't return the expected output. It will return even 40 as well which is not the desired output.

  • SELECT ID FROM dbo.Table

    GROUP BY ID

    HAVING COUNT(DISTINCT DepartmentId) > 1

  • THNKS FOR THE rEPLY 🙂

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply