我有两列(初级)[PLAYER_ID] [LEAUGE_ID]pl/sql:我该如何写这个查询?
是这样的:
Player_id League_id
2139 8
2153 8
2302 10
2441 8
2441 10
我试图找到谁在两个同盟
据起到同样的球员上面的表格,我试图只发现:
Player_id League_id_1 League_id_2
2441 8 10
在此先感谢!
我有两列(初级)[PLAYER_ID] [LEAUGE_ID]pl/sql:我该如何写这个查询?
是这样的:
Player_id League_id
2139 8
2153 8
2302 10
2441 8
2441 10
我试图找到谁在两个同盟
据起到同样的球员上面的表格,我试图只发现:
Player_id League_id_1 League_id_2
2441 8 10
在此先感谢!
如果你不介意它在行:
SELECT t.*
FROM myTable t
INNER JOIN
(
SELECT Player_id
FROM myTable
GROUP BY Player_id
HAVING COUNT(*) = (SELECT COUNT(DISTINCT(League_id)) FROM myTable)
) p ON t.Player_id = p.Player_id
这应返回:
Player_id League_id 2441 8 2441 10
谢谢你的帮助! – user1683987
看来,你在一排想要这个。所以,你可以使用下面的使用PIVOT
得到它在一排:
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) > 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;
,或者如果你没有访问PIVOT
功能,您可以使用CASE
语句汇总:
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) > 1
) t2
on t1.player_id = t2.player_id
) x
group by player_id;
如果你不希望它在一排,那么你可以使用内部子查询:
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) > 1
) t2
on t1.player_id = t2.player_id
@ user1683987你的问题在编辑之前已经被这个答复了。如果你有改动,不要不接受答案,而是建立一个新的答案。 –
如果您无法使用PIVOT(仅11克),并且要输出在一排所有League_id,试试这个:
DECLARE
v_owner VARCHAR2 (40);
v_player_id VARCHAR2 (40);
v_league_id VARCHAR2 (100);
v_league_id_total VARCHAR2 (1000);
/* First cursor */
CURSOR get_player_id
IS
SELECT DISTINCT player_id
FROM table1
ORDER BY player_id;
/* Second cursor */
CURSOR get_league_id
IS
SELECT league_id
FROM table1
WHERE player_id = v_player_id
ORDER BY league_id;
BEGIN
-- Open first cursor
OPEN get_player_id;
LOOP
FETCH get_player_id
INTO v_player_id;
v_league_id_total := '';
EXIT WHEN get_player_id%NOTFOUND;
-- Open 2nd cursor
OPEN get_league_id;
LOOP
FETCH get_league_id
INTO v_league_id;
EXIT WHEN get_league_id%NOTFOUND;
v_league_id_total := v_league_id_total || ' , ' || v_league_id;
END LOOP;
DBMS_OUTPUT.put_line (RPAD (v_player_id,
26,
' '
) ||
RPAD (v_league_id_total,
26,
' '
));
CLOSE get_league_id;
END LOOP;
CLOSE get_player_id;
EXCEPTION
WHEN OTHERS
THEN
raise_application_error (-20001,
'An error was encountered - ' ||
SQLCODE ||
' -ERROR- ' ||
SQLERRM);
END;
输出将是这样的:
2139 , 8
2153 , 8
2303 , 8 , 10 (I added one more record.)
2441 , 8 , 10 , 11 , 12 (I added 2 more records)
谢谢你的帮助! – user1683987
您使用的RDBMS是什么? – Taryn
我使用的是Oracle:PL/SQL – user1683987
什么版本的oracle? – Taryn