我有兩列(初級)[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