简体   繁体   English

在SQL中透视表

[英]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. 我有TableA,我想旋转它并想要输出表,如图所示。 Thanks 谢谢

I have a table #tableA 我有一张桌子#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. A列有ABCPQR唯一值。 Datetime column has multiple values. Datetime列具有多个值。

How can I get the following output? 如何获得以下输出?

Datetime all Datetime values from TableA in column. Datetime列中TableA中的所有Datetime值。

  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. 这是您应在报表层而不是SQL中执行的操作。

It is possible in SQL ( demo ) but not something that SQL is designed to do. 在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

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 为此,您需要根据上述内容和#TableA的日期生成动态SQL字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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