Search code examples
sql-servermultiple-columnsunpivot

Unpivoting multiple, repeated columns with multiple (denormalized) values


I have a table with multiple columns like this...

+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
| 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 |
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+

And I want to unpivot the columns into rows like this in 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 |
+-------+----------------+---------+-----------+----------+------------+

I googled and checked other posts related to this but unable to get three values. :(

Appreciate the help!


Solution

  • You won't need to necessarily use UNPIVOT here at all.

    You'll be able to UNION the three denormalized Orders back into a flattened output, like so:

    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

    (The column names are set by the first select in the UNION, and the ORDER is applied to the final UNIONed data)