2013-11-23 61 views
0
SELECT * 
    From (
      SELECT Count(Player_Score_Record.Player_ID) AS TotalC, 
        Player.First_Name 
      FROM Player 
      INNER JOIN (Match INNER JOIN Player_Score_Record 
         ON Match.Match_ID = Player_Score_Record.Match_ID) 
       ON Player.Player_ID = Player_Score_Record.Player_ID 
      WHERE (Match.M_Type='International' Or Match.M_Type='National') 
      GROUP BY Player.First_Name 
      ORDER BY Count(Player_Score_Record.Player_ID) DESC 
      ) A 
    Where A.TotalC = 3 ; 

我試過Max(Count(Player_Score_Record.Player_ID) As TotalC - 它也顯示一個錯誤。獲得參加國際或國內比賽最高數量的球員?

最新的嘗試是使用WHERE A.TotalC = (SELECT Max(TotalC) From A);。它顯示

ORA-00942:表或視圖不存在。

所以我只能直接分配3個數(加入的最大匹配數)。

+1

眼看着表結構會有所幫助,我想。 – slhck

+0

非常感謝,這是我第一次在這裏發佈問題。我很震驚,當我幾分鐘前發佈它時,我正在尋找方法來做到這一點 – user3025012

+0

您的子查詢是否按預期工作? – Adarsh

回答

0

我不完全相信你是否只想:

  • 的球員通過比賽場次數量排序的列表。

  • 與玩過更多匹配的玩家玩過儘可能多的比賽的玩家列表。

無論如何,您可以嘗試以下任一解決方案。請考慮這些SQL語句未經測試,因爲我目前無法訪問數據庫。


如果選擇1,你可以使用下面的SQL:

select Player.first_name, player_matches.player_id, player_matches.total_played 
    from (
     select Player_ID, count(Match_ID) TotalPlayed 
      from Player_Score_Record 
      join Match 
      on Match.Match_ID = Player_Score_Record.Match_ID 
     where (Match.M_Type='International' Or Match.M_Type='National') 
     group by Player_ID 
    ) player_matches 
    join Player on player_matches.player_id = player.player_id 
order by player_matches.total_played desc 

我們做這個選擇是什麼:

  1. 首先,我們得到的Player_ID名單和數量該玩家已經玩過的匹配。
  2. 然後我們加入Player以獲得我們想要的任何進一步數據。
  3. 最後我們點了它。您可能需要在此處過濾total_played

如果,另一方面,它是你想嘗試以下SQL選項2:

select Player.first_name, Player_ID, count(Match_ID) TotalPlayed 
    from Player_Score_Record 
    join Player 
    on Player_Score_Record.player_id = player.player_id 
group by Player_ID 
having count(Match_ID) = (
          select max(total_played) 
           from (
            select Player_ID, count(Match_ID) TotalPlayed 
             from Player_Score_Record 
             join Match 
             on Match.Match_ID = Player_Score_Record.Match_ID 
             where (Match.M_Type='International' Or Match.M_Type='National') 
             group by Player_ID 
            ) 

我們做這個選擇是什麼:

  1. 首先,我們獲取Player_ID的列表以及玩家已經玩過的比賽數量。
  2. 然後我們提取任何玩家玩過的最大匹配數。
  3. 最後,我們會獲得玩過最大比賽次數的玩家信息。
+0

非常感謝Guillem Vicens,選項2編碼就是我想要的。目前它仍然無法正常工作,但是這給了我一個關於編碼如何工作的好主意:D – user3025012

+0

請問您爲什麼還不能正常工作? –

0

兩個選項:

  • 問題1:如果你希望所有的Player_ID S的玩家所玩遊戲的最高數;或
  • 查詢2:如果你只想要一個玩家玩的遊戲數量最多的玩家。

SQL Fiddle

的Oracle 11g R2架構設置

CREATE TABLE Player 
(
    Player_ID PRIMARY KEY, 
    First_Name 
) AS 
      SELECT 1, 'A' FROM DUAL 
UNION ALL SELECT 2, 'A' FROM DUAL 
UNION ALL SELECT 3, 'A' FROM DUAL; 

CREATE TABLE Match 
(
    Match_ID PRIMARY KEY, 
    M_Type 
) AS 
      SELECT 1, 'National' FROM DUAL 
UNION ALL SELECT 2, 'International' FROM DUAL 
UNION ALL SELECT 3, 'League' FROM DUAL; 

CREATE TABLE Player_Score_Record 
(
    Player_ID NUMBER(1), 
    Match_ID NUMBER(1), 
    PRIMARY KEY (Player_ID, Match_ID), 
    FOREIGN KEY (Player_ID) REFERENCES Player (Player_ID), 
    FOREIGN KEY (Match_ID) REFERENCES Match (Match_ID) 
); 

INSERT INTO Player_Score_Record 
      SELECT 1,1 FROM DUAL 
UNION ALL SELECT 1,2 FROM DUAL 
UNION ALL SELECT 2,1 FROM DUAL 
UNION ALL SELECT 2,2 FROM DUAL 
UNION ALL SELECT 2,3 FROM DUAL 
UNION ALL SELECT 3,2 FROM DUAL 
UNION ALL SELECT 3,3 FROM DUAL; 

查詢1

WITH num_games AS (
    SELECT r.Player_ID, COUNT(1) AS number_of_games_played 
    FROM Player_Score_Record r 
     INNER JOIN 
     Match m 
     ON (r.Match_ID = m.Match_ID) 
    WHERE m.M_Type IN ('National', 'International') 
    GROUP BY r.Player_ID 
) 
SELECT Player_ID 
FROM num_games 
WHERE number_of_games_played = (SELECT MAX(number_of_games_played) 
           FROM num_games) 

Results

| PLAYER_ID | 
|-----------| 
|   1 | 
|   2 | 

查詢2

WITH num_games AS (
    SELECT r.Player_ID, COUNT(1) AS number_of_games_played 
    FROM Player_Score_Record r 
     INNER JOIN 
     Match m 
     ON (r.Match_ID = m.Match_ID) 
    WHERE m.M_Type IN ('National', 'International') 
    GROUP BY r.Player_ID 
    ORDER BY number_of_games_played DESC, r.Player_ID 
) 
SELECT Player_ID 
FROM num_games 
WHERE ROWNUM = 1 

Results

| PLAYER_ID | 
|-----------| 
|   1 |