2014-04-17 50 views
0

http://www.sqlfiddle.com/#!3/7a295/1SQL查詢得到最想要的遊戲細節

我有一個MS SQL數據庫的Visual Studio 10裏,我需要一個查詢,讓我從收藏表中最想要的遊戲,從遊戲cover_img一起幫助遊戲桌。我有查詢與第一部分工作: -

SELECT game_name, 
     game_platform, 
     Count(game_name) AS Expr1 
FROM favourites 
GROUP BY game_name, 
      game_platform 
HAVING (Count(game_name) = (SELECT Max(mycount) AS Expr1 
          FROM (SELECT game_name, 
              Count(game_name) AS mycount 
            FROM favourites AS Favourites_1 
            GROUP BY game_name) AS derivedtbl_1)); 

,但我不能讓cover_img以匹配遊戲桌最大的遊戲,有人可以幫助我,這是我到目前爲止,但它不工作: -

SELECT favourites.game_name, 
     favourites.game_platform, 
     games.cover_img, 
     Count(favourites.game_name) AS Expr1 
FROM games 
     inner join favourites 
       ON games.name = favourites.game_name 
GROUP BY favourites.game_name, 
      favourites.game_platform, 
      games.cover_img HAVING (Count(favourites.game_name) = (SELECT 
           Max(mycount) AS Expr1 
                   FROM 
           (SELECT game_name, 
             Count(game_name) AS mycount 
            FROM favourites AS Favourites_1 
            GROUP BY game_name) AS derivedtbl_1)); 
+0

您使用的是什麼RDBMS? – Mureinik

+0

@Mureinik,它是SQL Server。 – Rahul

+1

遊戲桌上有任何一種獨特的鑰匙嗎?我相信的基本問題是,你無法通過數據庫中的二進制字段進行分組。 – Sparky

回答

1

這是否你想要做什麼?

SELECT TOP 1 f.game_name, f.game_platform, g.cover_img, 
     Count(f.game_name) as cnt 
FROM games g inner join 
     favourites f 
     ON g.name = f.game_name 
GROUP BY f.game_name, f.game_platform, g.cover_img 
ORDER BY Count(f.game_name) desc; 
+0

這確實得到了cover_img,但它是XBOX ONE,我需要game.name = g.name和game_platform = g.platform。我是否會添加另一個ON語句Gorden?謝謝 – Alexandria

+0

@Alexandria。 。 。我不知道'games'和'favourites'之間的'join'條件是什麼。如果它需要兩個鍵,那麼是的,你可以將它加入'join'條件。 –

+0

嗨戈登我已經在sqlfiddle上創建了數據庫,並且您的查詢可以工作,但我只需要從遊戲桌獲取cover_img以匹配前1個遊戲。我試過ON g.name = f.game_name,g.platform = f.game_platform,但它不起作用。會提供任何幫助。感謝http://www.sqlfiddle.com/#!3/7a295/1 – Alexandria

1

你的第一個查詢將選擇你正在尋找的遊戲。

SELECT game_name, 
      game_platform, 
      Count(game_name) AS Expr1 
    FROM favourites 
    GROUP BY game_name, 
       game_platform 
    HAVING (Count(game_name) = (SELECT Max(mycount) AS Expr1 
           FROM (SELECT game_name, 
               Count(game_name) AS mycount 
             FROM favourites AS Favourites_1 
             GROUP BY game_name) AS derivedtbl_1)); 

現在你需要的是一個包裝查詢來查找所有與這些遊戲相關的圖像。

SELECT xx.game_name,xx.game_platform, games.cover_img 
from 
(SELECT game_name, 
     game_platform 
FROM favourites 
GROUP BY game_name, 
      game_platform 
HAVING (Count(game_name) = (SELECT Max(mycount) AS Expr1 
           FROM (SELECT game_name, 
             Count(game_name) AS mycount 
             FROM favourites AS Favourites_1 
             GROUP BY game_name) AS derivedtbl_1 
            ) 
          ) 
) xx 
join games on games.game_name=xx.game_name 
    and games.game_platform=xx.game_platform 

試試看。

2

可以在更小的部分分割你的問題:

; 
WITH Games  AS 
(
     SELECT * 
     FROM ( VALUES 
        ('Pacman', 'Atari', 'A'), 
        ('Enduro', 'Atari', 'B'), 
        ('River Raid', 'Atari', 'C') 
       ) AS X (Name, [Platform], cover_img)  
),  Favourites AS 
(
     SELECT * 
     FROM ( VALUES 
        ('Pacman', 'Atari'), 
        ('Pacman', 'Atari'), 
        ('Pacman', 'Atari'), 
        ('Enduro', 'Atari') 
       ) AS Y (game_name, game_platform) 
),  MostWanted AS 
( -- Identify the most wanted game, per platform 
     SELECT  game_name, game_platform, COUNT(*) Total, 
        ROW_NUMBER() OVER 
        (
         PARTITION BY /*game_name,*/ game_platform 
         ORDER BY COUNT(*) DESC 
        ) [Order] 
     FROM  Favourites 
     GROUP BY game_name, game_platform 
) 
SELECT MostWanted.*, Games.cover_img 
FROM MostWanted 
JOIN Games  -- JOIN that with games table to get more details 
    ON MostWanted.game_name = Games.Name 
WHERE MostWanted.[Order] = 1 -- Only the first most wanted 
+0

THanks,我會試試這個,是否將這個遊戲名稱與遊戲表和Game_platform和Platform的名稱相匹配,因爲此刻我得到了錯誤的平臺? THanks – Alexandria

+0

如果你可以添加一個數據樣本,這將有助於做更多的測試。 –

+0

我想把樣本放在sqlfiddle上,但我不能登陸網站,可以嗎?你知道一個類似的網站嗎?非常感謝 – Alexandria