John M
John M

Reputation: 201

SQL - Select non repeating columns

I have a table like

   id       name  R_id  mgr_id
   ----------------------------
    61        a     22    1
    62        a     22    2
    62        b     23    1
    63        c     24    4
    63        b     22    3
    64        c     25    3

and I would like to get the following result set

    R_id  mgr_id
   --------------
    22    1
    23    1
    24    4
    25    3

I would like select repeating R_ids only once

I tried using this query but with not much success, can anyone help me.

   SELECT DISTINCT R_id, mgr_id from DT

Upvotes: 2

Views: 177

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81960

Perhaps something like this... WITH TIES clause in concert with Row_NUmber()

Example

Select Top 1 with ties
       R_ID
      ,mgr_id
 From  @YourTable
 Order By Row_Number() over (Partition By R_ID order by Mgr_id)

Returns

R_ID    mgr_id
22      1
23      1
24      4
25      3

Upvotes: 4

Related Questions