2014-02-08 61 views
1

我需要獲得第一和最高分數爲每個game_id中列轉換多個行,每課:SQL:在同一行

-------------------------------------------- 
|id |lesson_id |game_id |score |date  | 
-------------------------------------------- 
|1 |1   |0  |20 |1391627323 | 
|2 |1   |0  |80 |1391627400 | 
|3 |1   |1  |5  |1391627543 | 
|4 |1   |2  |7  |1391627450 | 
|5 |2   |0  |90 |1391627323 | 
|6 |2   |1  |10 |1391628000 | 
|7 |2   |2  |8  |1391628005 | 
|8 |2   |2  |9  |1391628010 | 
|9 |2   |0  |95 |1391628333 | 
|10 |3   |0  |50 |1391627323 | 
-------------------------------------------- 

我需要這樣的輸出:

--------------------------------------------------- 
|   |game_id = 0 |game_id = 1 |game_id = 2 | 
|lesson_id |first |max |first |max |first |max | 
--------------------------------------------------- 
|1   |20 |80 |5  |5 |7  |7 | 
|2   |90 |95 |10 |10 |8  |9 | 
|3   |50 |50 |-  |- |-  |- | 
--------------------------------------------------- 

到目前爲止,我有這個,它得到一排每個game_id(第一和max),但很明顯,這些值必須在同一行中:

SELECT game_id, lesson_id, MAX(score) AS max_score, MIN(date), score AS first_score FROM cdu_user_progress 
GROUP BY game_id 

任何人提供任何幫助?

+0

看起來你需要PIVOT命令。請參閱http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query – mockaroodev

+0

您使用的是哪個數據庫? –

+0

我正在使用MySQL – user3241112

回答

0

我猜測你正在使用MySQL,因爲您的示例查詢將是大多數其他數據庫語法不正確。

你想有條件的聚集。第一次得分是棘手:

select lesson_id, 
     substring_index(group_concat(case when game_id = 1 then score end order by date), 
         ',', 1) as game1_first, 
     max(case when game_id = 1 then score end) as game2_max, 
     substring_index(group_concat(case when game_id = 2 then score end order by date), 
         ',', 1) as game2_first, 
     max(case when game_id = 2 then score end) as game1_max, 
     substring_index(group_concat(case when game_id = 3 then score end order by date), 
         ',', 1) as game3_first, 
     max(case when game_id = 3 then score end) as game3_max 
from cdu_user_progress 
GROUP BY lesson_id; 

如果你有數目不詳的遊戲,那麼你就必須構建SQL作爲一個字符串,然後用準備或其它接口來執行它。

0

如果你有game_id值的幾號,你可以嘗試這樣的事:

SELECT x.lesson_id, 
     MAX(CASE WHEN x.game_id=0 THEN x.scorefirst ELSE 0 END) AS first0, 
     MAX(CASE WHEN x.game_id=0 THEN y.score ELSE 0 END) AS max0, 
     MAX(CASE WHEN x.game_id=1 THEN x.scorefirst ELSE 0 END) AS first1, 
     MAX(CASE WHEN x.game_id=1 THEN y.score ELSE 0 END) AS max1, 
     MAX(CASE WHEN x.game_id=2 THEN x.scorefirst ELSE 0 END) AS first2, 
     MAX(CASE WHEN x.game_id=2 THEN y.score ELSE 0 END) AS max2, 
FROM (SELECT t1.lesson_id, t1.game_id, t1.score AS score first, t1.date 
     FROM cdu_user_progress t1 INNER JOIN 
       cdu_user_progress t2 ON t1.lesson_id=t2.lesson_id 
            AND t1.game_id =t2.game_id AND t1.date>=t2.date 
     GROUP BY t1.lesson_id, t1.game_id, t1.score, t1.date 
     HAVING COUNT(*)=1) x INNER JOIN 
     cdu_user_progress y ON x.lesson_id=y.lesson_id AND x.game_id = y.game_id 
GROUP BY x.lesson_id 

否則你應該做它編程

0

我可能會做這兩個步驟 - 首先,我'以行格式獲取結果,然後使用另一個查詢中的結果將它們移動到列中。

 SELECT g.lesson_id, 
       g.game_id, 
       c.score AS first, 
       g.max_score  
     FROM (
      SELECT lesson_id, 
        game_id, 
        MAX(score) AS max_score, 
        MIN(date) AS min_date  
       FROM cdu_user_progress 
      GROUP BY lesson_id, game_id 
      ) g  
     LEFT JOIN dbo.cdu_user_progress c 
     ON g.lesson_id = c.lesson_id 
     AND g.game_id = c.game_id 
     AND g.min_date = c.date 

沒有,這取決於你希望有多少列,你會寫周圍的查詢到他們拉入列。注:在SQL Server中,可能值得查看PIVOT命令。

ex。

SELECT lesson_id, 
     MAX(CASE WHEN p.game_id = 0 
      THEN p.first 
      END) AS game_id0_first, 
     MAX(CASE WHEN p.game_id = 0 
      THEN p.max_score 
      END) AS game_id0_max 
     -- Add other columns here  
FROM (
    SELECT g.lesson_id, 
      g.game_id, 
      c.score AS first, 
      g.max_score  
     FROM (
      SELECT lesson_id, 
        game_id, 
        MAX(score) AS max_score, 
        MIN(date) AS min_date  
       FROM cdu_user_progress 
      GROUP BY lesson_id, game_id 
      ) g  
     LEFT JOIN dbo.cdu_user_progress c 
     ON g.lesson_id = c.lesson_id 
     AND g.game_id = c.game_id 
     AND g.min_date = c.date 
    ) p 
GROUP BY p.lesson_id