简体   繁体   中英

Pivoting a table in sql

I want same output shown in Output table. I have TableA and I want to pivote it and want output table as shown in image. Thanks

I have a table #tableA

+-----+-------------+-------+------------+
|  A  | Allocations | Seats |  EndDate   |
+-----+-------------+-------+------------+
| ABC |         450 |    23 | 2017-10-05 |
| ABC |          23 |   765 | 2017-05-01 |
| PQR |          54 |    34 | 2017-07-04 |
| ABC |         234 |    45 | 2017-11-27 |
| PQR |         987 |    76 | 2017-03-05 |
| ABC |          76 |    65 | 2017-02-23 |
| PQR |          89 |   324 | 2017-08-14 |
| ABC |          45 |    34 | 2017-07-13 |
+-----+-------------+-------+------------+

Which can be created and populated as below.

CREATE TABLE #TableA
  (
     A           VARCHAR(50),
     Allocations INT,
     Seats       INT,
     EndDate     DATETIME
  );

INSERT INTO #TableA
VALUES     ('ABC',450,23,'2017-10-05'),
           ('ABC',23,765,'2017-05-01'),
           ('PQR',54,34,'2017-07-04'),
           ('ABC',234,45,'2017-11-27'),
           ('PQR',987,76,'2017-03-05'),
           ('ABC',76,65,'2017-02-23'),
           ('PQR',89,324,'2017-08-14'),
           ('ABC',45,34,'2017-07-13'); 

A column has ABC and PQR unique values. Datetime column has multiple values.

How can I get the following output?

Datetime all Datetime values from TableA in column.

  Output    :-

    date         |  2017-12-13   |  2017-12-20  |   2017-12-27 | -|-|-|-|-|-|-|-|-|
    ------------------------------------------------------------------------------- 
    A            |  ABC          |  ABC         |   ABC        |
    Allocations  |  50           |  50          |   50         |
    Seats        |  27           |  27          |   27         |
    A            |  PQR          |  PQR         |   PQR        |
    Alloc        |  50           |  50          |   50         |
    Seats        |  12           |  12          |   12         |

This is something that you should do in the reporting layer not SQL.

It is possible in SQL ( demo ) but not something that SQL is designed to do.

WITH T
     AS (SELECT A,
                thing,
                priority,
                value,
                d
         FROM   #TableA
                CROSS APPLY (VALUES(CAST(EndDate AS DATE)))D(d)
                CROSS APPLY (VALUES(1, 'A', NULL),
                                   (2, 'Allocations', Allocations),
                                   (3, 'Seats', Seats)) V(priority, thing, value))
SELECT thing, 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-02-23],0) AS VARCHAR(50)) END AS [2017-02-23],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-03-05],0) AS VARCHAR(50)) END AS [2017-03-05],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-05-01],0) AS VARCHAR(50)) END AS [2017-05-01],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-07-04],0) AS VARCHAR(50)) END AS [2017-07-04],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-07-13],0) AS VARCHAR(50)) END AS [2017-07-13],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-08-14],0) AS VARCHAR(50)) END AS [2017-08-14],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-10-05],0) AS VARCHAR(50)) END AS [2017-10-05],
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-11-27],0) AS VARCHAR(50)) END AS [2017-11-27]
FROM T
PIVOT (SUM(value) FOR d in (
                            [2017-02-23],
                            [2017-03-05],
                            [2017-05-01],
                            [2017-07-04],
                            [2017-07-13],
                            [2017-08-14],
                            [2017-10-05],
                            [2017-11-27])) P
ORDER BY A, priority

The above doesn't even address the dynamic aspect. For that you would need to generate a dynamic SQL string based on the above and the dates in #TableA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM