2017-09-16 93 views
0

我查看了Mysql query to dynamically convert rows to columns的答案,但由於我需要連接表以獲取所有信息,所以我無法使其工作。你能幫我把'問題'作爲列名嗎?Mysql/Mariadb查詢在查詢數據時動態地將行轉換爲列

datestamp   pay  benefits ... career advancement 
--------------------------------------------------------------------- 
2/16/2017 11:55  Somewhat Slightyly  Slightly 
2/16/2017 11:55  Agree  Somewhat  Very 

的數據是這樣的:

id datestamp  survey_col   value 
----------------------------------------------- 
1 2/16/2017 11:55 885457X234X1368SQ001 A3 
1 2/16/2017 11:55 885457X234X1368SQ002 A4 
1 2/16/2017 11:55 885457X234X1368SQ003 A4 
1 2/16/2017 11:55 885457X234X1368SQ004 A3 
1 2/16/2017 11:55 885457X234X1368SQ005 A2 
1 2/16/2017 11:55 885457X234X1368SQ006 A3 
1 2/16/2017 11:55 885457X234X1368SQ007 A4 
1 2/16/2017 11:55 885457X234X1368SQ008 A3 
1 2/16/2017 11:55 885457X234X1368SQ009 A4 
1 2/16/2017 11:55 885457X234X1368SQ010 A1 

我使用這個代碼:

SELECT T.id, T.datestamp, SQ.question, 
     IF(type IN ("K", "N", "S", "T", "Y", "*") AND type NOT IN ("F"), 
     T.value, 
     IF(parent_qid = 0, SA.answer, SA2.answer) 
    ) as answer 
     FROM survey_questions SQ 
     JOIN survey_lookup SL ON SL.qid = SQ.qid 
     JOIN tmp T ON T.survey_col = SL.survey_col 
     LEFT JOIN survey_answers SA ON SA.qid = SQ.qid 
     AND SA.code = T.value 
     AND SA.language = 'en' 
     LEFT JOIN survey_answers SA2 on SA2.qid = SQ.parent_qid 
     AND SA2.code = T.value 
     AND SA2.language = 'en' 
     WHERE SQ.language = 'en' 
     ; 

其中規定:

id datestamp  question       answer 
---------------------------------------------------------------- 
1 2/16/2017 11:55 Pay         Somewhat 
1 2/16/2017 11:55 Benefits (health plan, leave, etc.) Slightly 
1 2/16/2017 11:55 Career Advancement     Slightly 
1 2/16/2017 11:55 Access to Training     Somewhat 
1 2/16/2017 11:55 Leadership Style     Very 
1 2/16/2017 11:55 Manager/Supervisor Style   Somewhat 
1 2/16/2017 11:55 Please enter comments    The company's policies... 

此代碼工作時,所有的數據在一張桌子上,我不需要你se加入。

SET @sql = NULL; 
    SET SESSION GROUP_CONCAT_MAX_LEN = 1000000; -- default is 1024 
    SELECT 
     GROUP_CONCAT(DISTINCT 
     CONCAT(
      'MAX(IF(question = ''', REPLACE(question,"'", "\\'"), ''', answer, NULL)) AS ''', REPLACE(question,"'", "\\'"), '''' 
     ) 
    ) INTO @sql 
     FROM tmp; 

     SET @sql = CONCAT('SELECT row_id, submitdate, ', @sql, ' FROM ', survey_report, ' GROUP BY row_id'); 

     IF DEBUG = 1 THEN 
     SELECT @sql; 
     END IF; 

     PREPARE stmt FROM @sql; 
     EXECUTE stmt; 
     DEALLOCATE PREPARE stmt; 

這是一個沒有REPLACE功能的嘗試(我想使用它)。

SELECT 
    GROUP_CONCAT(DISTINCT 
    CONCAT(
    'MAX(IF(SQ.question = ', '''SQ.question''',' 
     IF(type IN ("K", "N", "S", "T", "Y", "*") AND type NOT IN ("F"), 
     T.value, 
     IF(parent_qid = 0, SA.answer, SA2.answer) 
    ) as answer, NULL)) as answer', '''SQ.question''') 
) INTO @sql 
    FROM survey_questions SQ 
    JOIN survey_lookup SL ON SL.qid = SQ.qid 
    JOIN tmp T ON T.survey_col = SL.survey_col 
    LEFT JOIN survey_answers SA ON SA.qid = SQ.qid 
    AND SA.code = T.value 
    AND SA.language = 'en' 
    LEFT JOIN survey_answers SA2 on SA2.qid = SQ.parent_qid 
    AND SA2.code = T.value 
    AND SA2.language = 'en' 
    WHERE SQ.language = 'en'; 

    SET @sql = CONCAT('SELECT id, datestamp, ', @sql, ' FROM tmp GROUP BY id'); 

    SELECT @sql; 

    PREPARE stmt FROM @sql; 
    EXECUTE stmt; 
    DEALLOCATE PREPARE stmt; 
+1

您可能已經知道這稱爲* pivoting *表。你已經發現它在MySQL中是多麼的痛苦。 –

+0

我同意。我正在使用非常具有挑戰性的軟件來獲取所需的數據。我使用遊標來獲取非常緩慢的數據,但是我發現工會使它快得多。所以,我使用聯盟來獲取數據,以便我可以創建聯接。現在,我正在試圖將它應用於我可以在圖形軟件中使用的格式。我嘗試了幾件事情,這似乎是最好的,因爲它運行了11秒,其他解決方案我花了4分鐘。 –

+1

這是SQL解決問題的方法之一。編寫PHP/Java /任何代碼來進行格式化。使用SQL來提供數據。 –

回答

0

這工作,但我不想創建另一個表。

DROP TEMPORARY TABLE IF EXISTS `tmp2`; 
CREATE TEMPORARY TABLE tmp2 
    SELECT T.id, T.datestamp, SQ.question, 
     IF(type IN ("K", "N", "S", "T", "Y", "*") AND type NOT IN ("F"), 
     T.value, 
     IF(parent_qid = 0, SA.answer, SA2.answer) 
    ) as answer 
     FROM survey_questions SQ 
     JOIN survey_lookup SL ON SL.qid = SQ.qid 
     JOIN tmp T ON T.survey_col = SL.survey_col 
     LEFT JOIN survey_answers SA ON SA.qid = SQ.qid 
     AND SA.code = T.value 
     AND SA.language = 'en' 
     LEFT JOIN survey_answers SA2 on SA2.qid = SQ.parent_qid 
     AND SA2.code = T.value 
     AND SA2.language = 'en' 
     WHERE SQ.language = 'en' 
     ; 

- select * from tmp2;

  SET @sql = NULL; 
    SET SESSION GROUP_CONCAT_MAX_LEN = 1000000; -- default is 1024 
    SELECT 
     GROUP_CONCAT(DISTINCT 
     CONCAT(
      'MAX(IF(question = ''', REPLACE(question,"'", "\\'"), ''', answer, NULL)) AS ''', REPLACE(question,"'", "\\'"), '''' 
     ) 
    ) INTO @sql 
     FROM tmp2; 
     SET @sql = CONCAT('SELECT id, datestamp, ', @sql, ' FROM tmp2 GROUP BY id'); 

     SELECT @sql; 

     PREPARE stmt FROM @sql; 
     EXECUTE stmt; 
     DEALLOCATE PREPARE stmt;