我有一个包含每人一行的大型数据集,以及可能填充或未填充的25个标准列,如下例所示:
SELECT * FROM Criteria_Input;
╔══════════╦═══════════╦═══════════╦═══════════╦═══════════╦═══════════╦═══════════╗
║ Name ║ Criteria1 ║ Criteria2 ║ Criteria3 ║ Criteria4 ║ Criteria5 ║ Criteria6 ║
╠══════════╬═══════════╬═══════════╬═══════════╬═══════════╬═══════════╬═══════════╣
║ Michael ║ ║ Yes ║ ║ ║ Yes ║ ║
║ Brant ║ ║ ║ ║ ║ ║ Yes ║
║ Mary ║ Yes ║ ║ ║ Yes ║ ║ ║
║ John ║ ║ ║ ║ ║ Yes ║ ║
║ Connie ║ ║ ║ Yes ║ Yes ║ ║ ║
╚══════════╩═══════════╩═══════════╩═══════════╩═══════════╩═══════════╩═══════════╝
我需要将这些数据转换为如下行:
SELECT * FROM Criteria_Final;
╔═════════╦═══════════╗
║ Name ║ Criteria ║
╠═════════╬═══════════╣
║ Michael ║ Criteria2 ║
║ Michael ║ Criteria5 ║
║ Brant ║ Criteria6 ║
║ Mary ║ Criteria1 ║
║ Mary ║ Criteria4 ║
║ John ║ Criteria5 ║
║ Connie ║ Criteria3 ║
║ Connie ║ Criteria4 ║
╚═════════╩═══════════╝
甚至更好,这是:
SELECT * FROM Criteria_Final;
╔═════════╦══════════╗
║ Name ║ Criteria ║
╠═════════╬══════════╣
║ Michael ║ 2 ║
║ Michael ║ 5 ║
║ Brant ║ 6 ║
║ Mary ║ 1 ║
║ Mary ║ 4 ║
║ John ║ 5 ║
║ Connie ║ 3 ║
║ Connie ║ 4 ║
╚═════════╩══════════╝
这种数据转换在MySQL中是否可行?我需要进行这种转换的原因是我将结果表与标准号上的另一个表一起加入。
答案 0 :(得分:3)
尝试使用UNIONs进行查询 -
SELECT * FROM (
SELECT name, 1 AS criteria FROM cr WHERE LENGTH(Criteria1) > 0
UNION
SELECT name, 2 AS criteria FROM cr WHERE LENGTH(Criteria2) > 0
UNION
SELECT name, 3 AS criteria FROM cr WHERE LENGTH(Criteria3) > 0
UNION
SELECT name, 4 AS criteria FROM cr WHERE LENGTH(Criteria4) > 0
UNION
SELECT name, 5 AS criteria FROM cr WHERE LENGTH(Criteria5) > 0
UNION
SELECT name, 6 AS criteria FROM cr WHERE LENGTH(Criteria6) > 0
) t
ORDER BY name, criteria
更改WHERE条件以在需要时检查NULL值。
答案 1 :(得分:1)
这是我的解决方案,基于@Devart的漂亮解决方案。首先,创建输入表:
CREATE TABLE Criteria_Input (
Name VARCHAR(10),
Criteria1 VARCHAR(5),
Criteria2 VARCHAR(5),
Criteria3 VARCHAR(5),
Criteria4 VARCHAR(5),
Criteria5 VARCHAR(5),
Criteria6 VARCHAR(5),
Criteria7 VARCHAR(5),
Criteria8 VARCHAR(5)
);
接下来,创建最终表:
CREATE TABLE Criteria_Final (
Name VARCHAR(10),
Criteria INT(2)
);
然后插入样本数据:
INSERT INTO Criteria_Input
(`Name`, `Criteria1`, `Criteria2`, `Criteria3`, `Criteria4`, `Criteria5`, `Criteria6`)
VALUES
('"Michael"', NULL, '"Yes"', NULL, NULL, '"Yes"', NULL),
('"Brant"', NULL, NULL, NULL, NULL, NULL, '"Yes"'),
('"Mary"', '"Yes"', NULL, NULL, '"Yes"', NULL, NULL),
('"John"', NULL, NULL, NULL, NULL, '"Yes"', NULL),
('"Connie"', NULL, NULL, '"Yes"', '"Yes"', NULL, NULL)
;
最后,转换数据的代码:
INSERT INTO Criteria_Final
SELECT * FROM (
SELECT name, 1 AS Criteria FROM Criteria_Input WHERE Criteria1 IS NOT NULL
UNION
SELECT name, 2 AS Criteria FROM Criteria_Input WHERE Criteria2 IS NOT NULL
UNION
SELECT name, 3 AS Criteria FROM Criteria_Input WHERE Criteria3 IS NOT NULL
UNION
SELECT name, 4 AS Criteria FROM Criteria_Input WHERE Criteria4 IS NOT NULL
UNION
SELECT name, 5 AS Criteria FROM Criteria_Input WHERE Criteria5 IS NOT NULL
UNION
SELECT name, 6 AS Criteria FROM Criteria_Input WHERE Criteria6 IS NOT NULL
) Criteria_Input
ORDER BY Name, Criteria;
您可以在SQL Fiddle上看到这一点。