2017-02-22 67 views
-2

我有一個匹配的勝利隊和stadiumid作爲屬性, 我需要檢索贏得了所有比賽在同一體育場贏得勝利。解決方案爲寫作查詢

我想這一點,並且我得到額外的不利行:

select winningteamid 
from match 
group by winningteamid 
having count(winningteamid) in (
    select count(*) from match group by (winningteamid,stadiumid) 
+0

語法錯誤... – jarlh

+0

添加一些示例表格數據和預期結果 - 以及格式化文本。 – jarlh

回答

0

你應該使用這樣的:

SELECT MAX(winningteamid) 
FROM (
    SELECT DISTINCT winningteamid, stadiumid 
    FROM match 
) 
GROUP BY stadiumid 
HAVING COUNT(*) = 1; 
+0

我正在使用oracle – nagesh

+0

謝謝..全部 – nagesh

0

試試這個,請(請寫出您所使用的RDBMS):

with cte as (
    select winningteamid, stadiumid, count(stadiumid) over (partition by winningteamid) as count 
    from match 
    group by winningteamid, stadiumid 
) 
select * from cte where count = 1; 
+0

謝謝..運行良好 – nagesh

+0

歡迎您,如果這是解決方案,請投票或接受答案 – RoundFour

0

我想這和使用HAVING來驗證只有一個不同的場館標識一樣簡單:

select winningteamid 
from match 
group by winningteamid 
having count(distinct stadiumid) = 1 
+0

我使用的是oracle 9i,match屬性matchid,winningteamid,stadiumid – nagesh