Search code examples
sqlunique-values

SQL - Select non duplicates column values


I have the following SQL table containing a group ID, and the users that needs access to the groups. I have 3 types of users, but if it is the same user in all 3 types, then I only need to select 1 and not all 3.

This is my table:

This is my table:

And this is the result I would like from the select

Result

Any help is appreciated :)


Solution

  • You can use 3 selects with union:

    select group_id,
           csp
      from your_table
    union
    select group_id,
           ep
      from your_table
    union
    select group_id,
           em
      from your_table
    order by 1, 2;
    

    Union will eliminate the duplicates.