簡體   English   中英

MySQL將多行表轉換為唯一的行表

[英]MySQL pivot multi-row table to unique row table

我在MySQL中有以下結果數據表。 我想將每個主題等級的每UPN行連接到每個UPN一行,以便下表:

UPN        Collection      Subject        Grade
1          Target          English        5
1          Current         English        6
1          Target          Maths          7
1          Current         Maths          7
1          Target          Art            6
1          Current         Art            6
2          Target          English        5
2          Current         English        5
2          Target          Maths          6
2          Current         Maths          7
2          Target          History        6
2          Current         History        5

應轉到下表:

UPN    English Current    English Target    Maths Current    Maths Target    Art Current    Art Target    History Current    History Target
1      6                  5                 7                7               6              6             NULL               NULL
2      5                  5                 7                6               NULL           NULL          5                  6

請注意,在第二個表中,UPN行必須是唯一的,因此沒有包含NULL的重復UPN行。

此外,如果UPN沒有學生,則單元格值應為NULL。

SQL小提琴

這是典型的pviot表問題。 請檢查以下查詢:

SET @sql := '';

SELECT 
CONCAT('SELECT upn, ',
GROUP_CONCAT(t.sql_code),
' FROM results GROUP BY upn;'
) INTO @sql
FROM 
(
SELECT 
GROUP_CONCAT('MAX(CASE WHEN Collection =\'', Collection ,'\' AND Subject =\'', Subject ,'\' THEN Grade END) AS \'',CONCAT(Collection,' ',Subject),'\'') AS sql_code
FROM results
GROUP BY Collection,Subject
) AS t;

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

請嘗試以下查詢

SELECT t1.UPN,
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Current' AND t2.`Subject` = 'English'),NULL) AS 'English Current',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Target' AND t2.`Subject` = 'English'),NULL) AS 'English Target', 
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Current' AND t2.`Subject` = 'Maths'),NULL) AS 'Maths Current',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Target' AND t2.`Subject` = 'Maths'),NULL) AS 'Maths Target',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Current' AND t2.`Subject` = 'Art'),NULL) AS 'Art Current',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Target' AND t2.`Subject` = 'Art'),NULL) AS 'Art Target',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Current' AND t2.`Subject` = 'History'),NULL) AS 'History Current',
IFNULL((SELECT MAX(t2.Grade) FROM `results` AS t2 WHERE t2.UPN = t1.UPN AND t2.`Collection` = 'Target' AND t2.`Subject` = 'History'),NULL) AS 'History Target'
FROM `results` AS t1 GROUP BY t1.`UPN`

@Matt我認為這將解決你的問題。

您將在SQL Fiddle中看到

SELECT tb.UPN,(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Current' AND tb1.`Subject` = 'English') AS 'English Current',(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Target' AND tb1.`Subject` = 'English') AS 'English Target', 
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Current' AND tb1.`Subject` = 'Maths') AS 'Maths Current',
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Target' AND tb1.`Subject` = 'Maths') AS 'Maths Target',
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Current' AND tb1.`Subject` = 'Art') AS 'Art Current',
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Target' AND tb1.`Subject` = 'Art') AS 'Art Target',
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Current' AND tb1.`Subject` = 'History') AS 'History Current',
(SELECT MAX(tb1.Grade) FROM `results` AS tb1 WHERE tb1.UPN = tb.UPN AND tb1.`Collection` = 'Target' AND tb1.`Subject` = 'History') AS 'History Target'
FROM `results` AS tb GROUP BY tb.`UPN`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM