使用多个(非规范化)值取消隐藏多个重复列

时间:2017-11-02 06:47:21

标签: sql-server multiple-columns unpivot

我有一个包含多列这样的表......

+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
| Name  |     Email      | Address | Order1  | Shipping1 |   Date1    |  Order2   | Shipping2 |   Date2    | Order3  | Shipping3 |   Date3    |
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
| John  | john@abcd.com  |     123 | Rock    |       123 | 02/11/2017 | Computer  |       123 | 07/11/2017 | Pen     |       123 | 12/11/2017 |
| Jane  | jane@abcd.com  |     234 | Scissor |       234 | 03/11/2017 | Laptop    |       234 | 08/11/2017 | Pencil  |       234 | 13/11/2017 |
| Julie | julie@abcd.com |     345 | Paper   |       345 | 04/11/2017 | Mouse     |       345 | 09/11/2017 | Clips   |       345 | 14/11/2017 |
| Jaden | jaden@abcd.com |     456 | Spock   |       456 | 05/11/2017 | Screen    |       456 | 10/11/2017 | Pins    |       456 | 15/11/2017 |
| Jabba | jabba@abcd.com |     678 | Lizard  |       678 | 06/11/2017 | Pen Drive |       678 | 11/11/2017 | Notepad |       678 | 16/11/2017 |
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+

我想在T-SQL中将列拆分为这样的行......

+-------+----------------+---------+-----------+----------+------------+
| Name  |     Email      | Address |   Order   | Shipping |    Date    |
+-------+----------------+---------+-----------+----------+------------+
| John  | john@abcd.com  |     123 | Rock      |      123 | 02/11/2017 |
| John  | john@abcd.com  |     123 | Computer  |      123 | 07/11/2017 |
| John  | john@abcd.com  |     123 | Pen       |      123 | 12/11/2017 |
| Jane  | jane@abcd.com  |     234 | Scissor   |      234 | 03/11/2017 |
| Jane  | jane@abcd.com  |     234 | Laptop    |      234 | 08/11/2017 |
| Jane  | jane@abcd.com  |     234 | Pencil    |      234 | 13/11/2017 |
| Julie | julie@abcd.com |     345 | Paper     |      345 | 04/11/2017 |
| Julie | julie@abcd.com |     345 | Mouse     |      345 | 09/11/2017 |
| Julie | julie@abcd.com |     345 | Clips     |      345 | 14/11/2017 |
| Jaden | jaden@abcd.com |     456 | Spock     |      456 | 05/11/2017 |
| Jaden | jaden@abcd.com |     456 | Screen    |      456 | 10/11/2017 |
| Jaden | jaden@abcd.com |     456 | Pins      |      456 | 15/11/2017 |
| Jabba | jabba@abcd.com |     678 | Lizard    |      678 | 06/11/2017 |
| Jabba | jabba@abcd.com |     678 | Pen Drive |      678 | 11/11/2017 |
| Jabba | jabba@abcd.com |     678 | Notepad   |      678 | 16/11/2017 |
+-------+----------------+---------+-----------+----------+------------+

我用谷歌搜索并检查了与此相关的其他帖子,但无法获得三个值。 :(

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您根本无需在此使用UNPIVOT

你将能够将三个非规范化的订单UNION回到扁平化输出中,如下所示:

SELECT name, Email, Address, Order1 AS Order, Shipping1 as Shipping, Date1 AS Date
FROM Table1
UNION ALL
SELECT name, Email, Address, Order2, Shipping2, Date2
FROM Table1
UNION ALL
SELECT name, Email, Address, Order3, Shipping3, Date3
FROM Table1
ORDER BY Name, Date;

SqlFiddle here

(列名由UNION中的第一个选择设置,ORDER应用于最终的UNIONed数据)