2014-02-21 49 views
0

我在完成此透視圖時遇到問題。我正在嘗試各種各樣的東西,但我似乎無法得到正確的結果。但是因爲我的運氣通常會運行,所以我可能不得不再次重新開始。MySql數據透視表不會聚集成組的行

我在這裏看了一下stackoverflow,但似乎沒有適合我的情況。盡我所知,我讀過的其他問題都是針對所有數字數據的。我有數字和字符串的混合可能是問題,但我只需要更多的眼睛就可以了。

我正在寫一份報告,該報告將顯示系統中每個用戶的一行以及他們每次課程的得分。旋轉的列表示「課程模塊」,並顯示分數,「未開始」或「不完整」。

以下是幾個查詢的結果。這是正確的:

「結果」

+---------+-----------+--------------+-------------+ 
| user_id | module_id | total_failed | total_taken | 
+---------+-----------+--------------+-------------+ 
|  605 |  89 |   0 |   0 | 
|  605 |  90 |   0 |   0 | 
|  605 |  91 |   0 |   0 | 
|  606 |  89 |   2 |   3 | 
|  606 |  90 |   1 |   4 | 
|  606 |  91 |   0 |   0 | 
|  634 |  89 |   0 |   3 | 
|  634 |  90 |   0 |   4 | 
|  634 |  91 |   0 |   0 | 
+---------+-----------+--------------+-------------+ 

正是有了這種記錄集,我需要轉動。

user_id將保留在第一列中,接下來的三列將轉到用戶的結果中,其列數等於課程數量。在這個例子中,將有三列的教訓(模塊)89,90和91

我創建一個函數"getModuleScore"返回的三個可能的值之一:用戶的得分(0.0 =< x =< 1.0),或兩個文字中的一個:「未開始「或」不完整「。 (這是對這個函數做什麼的簡單解釋 - 比我剛纔寫的還要多一點。)

如果我使用下面的sql來轉動以前的結果(SQL中的「結果」表)

SELECT 
    results.user_id AS user_id 
    , 
     MAX(CASE WHEN module_id = 89 
     THEN (SELECT getModuleScore(89, total_taken, total_failed)) 
     ELSE 
      'not_started' 
     END) 
     AS `mod_89_score` 
    , 
     MAX(CASE WHEN module_id = 90 
     THEN (SELECT getModuleScore(90, total_taken, total_failed)) 
     ELSE 
      'not_started' 
     END) 
     AS `mod_90_score` 
    , 
     MAX(CASE WHEN module_id = 91 
     THEN (SELECT getModuleScore(91, total_taken, total_failed)) 
     ELSE 
      'not_started' 
     END) 
     AS `mod_91_score` 

FROM 
    results 

GROUP BY 
    results.user_id 
    , results.module_id 

我收到以下記錄集。

+---------+--------------+--------------+--------------+ 
| user_id | mod_89_score | mod_90_score | mod_91_score | 
+---------+--------------+--------------+--------------+ 
|  605 | not started | not_started | not_started | 
|  605 | not_started | not started | not_started | 
|  605 | not_started | not_started | not started | 
|  606 | 0.333333333 | not_started | not_started | 
|  606 | not_started | 0.750000000 | not_started | 
|  606 | not_started | not_started | not started | 
|  634 | 1.000000000 | not_started | not_started | 
|  634 | not_started | 1.000000000 | not_started | 
|  634 | not_started | not_started | not started | 
+---------+--------------+--------------+--------------+ 

我想是這樣的:

+---------+--------------+--------------+--------------+ 
| user_id | mod_89_score | mod_90_score | mod_91_score | 
+---------+--------------+--------------+--------------+ 
|  605 | not started | not_started | not_started | 
|  606 | 0.333333333 | 0.750000000 | not_started | 
|  634 | 1.000000000 | 1.000000000 | not_started | 
+---------+--------------+--------------+--------------+ 

我已經試過:

  1. 我已經reworte內查詢給我正確的數據來計算與
  2. 在MAX(),MIN()和SUM()中封入CASE子句
  3. 刪除了CASE子句中的ELSE子句,小時之後獲得我NULLS在結果集中
  4. 改變了GROUP BY子句
+1

不要'GROUP BY' module_id – AgRizzo

+0

@AgRizzo,就是這樣。非常感謝你。去看看我是如何看待這個太久。 – dmbania

回答

0

@AgRizzo了對我來說。

當我從GROUP BY子句中刪除res.module_id時,結果就是我正在尋找的。