Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It appears that you want this in row one. So you can use the following to get it in one row using a <code>PIVOT</code>:</p> <pre><code>select player_id, league_id_1, league_id_2 from ( select t1.player_id, t1.league_id, row_number() over(partition by t1.player_id order by t1.league_id) rn from table1 t1 inner join ( select player_id from table1 group by player_id having count(distinct league_id) &gt; 1 ) t2 on t1.player_id = t2.player_id ) x pivot ( max(league_id) for rn in (1 as league_id_1, 2 as league_id_2) ) p; </code></pre> <p>See <a href="http://sqlfiddle.com/#!4/8395c/10">SQL Fiddle with Demo</a></p> <p>or if you don't have access to the <code>PIVOT</code> function, you can use a <code>CASE</code> statement with aggregate:</p> <pre><code>select player_id, max(case when rn = 1 then league_id end) league_id_1, max(case when rn = 2 then league_id end) league_id_2 from ( select t1.player_id, t1.league_id, row_number() over(partition by t1.player_id order by t1.league_id) rn from table1 t1 inner join ( select player_id from table1 group by player_id having count(distinct league_id) &gt; 1 ) t2 on t1.player_id = t2.player_id ) x group by player_id; </code></pre> <p>See <a href="http://sqlfiddle.com/#!4/0145d/1">SQL Fiddle with Demo</a></p> <p>If you don't want it in one row, then you can just use the inner sub-query:</p> <pre><code> select t1.player_id, t1.league_id from table1 t1 inner join ( select player_id from table1 group by player_id having count(distinct league_id) &gt; 1 ) t2 on t1.player_id = t2.player_id </code></pre> <p>See <a href="http://sqlfiddle.com/#!4/0145d/4">SQL Fiddle with Demo</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload