在sql中透视表

时间:2017-10-14 17:58:24

标签: sql sql-server sql-server-2008 sql-server-2008-r2 sql-server-2014

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

我有一张表#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 |
+-----+-------------+-------+------------+

可以创建和填充以下内容。

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列包含ABCPQR个唯一值。 Datetime列有多个值。

如何获得以下输出?

日期时间列中TableA的所有日期时间值。

  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         |

1 个答案:

答案 0 :(得分:0)

您应该在报告层中执行此操作而不是SQL。

可以在SQL(demo)中使用,但不是SQL的目的。

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

以上内容甚至没有涉及动态方面。为此,您需要根据上述内容和#TableA

中的日期生成动态SQL字符串