动态反透视和拆分列 SQL Server 2012

Posted

技术标签:

【中文标题】动态反透视和拆分列 SQL Server 2012【英文标题】:Dynamic Unpivot and split Columns SQL Server 2012 【发布时间】:2015-10-20 12:31:03 【问题描述】:

我有一个具有 20 列的 MarketOutput 表

[Region] [LOB]  [GWP 2013]  [GWP 2014]  [LR 2013]   [LR 2014]
-------------------------------------------------------------
North  Workers  38902.50     37,972,404   89             82

我想动态地将列更改为行。 Region和LOB是固定列,[GWP 2013]、[GWP 2014]、[LR 2013]、[LR 2014]是动态列。

明年他们将是[GWP 2015][LR 2015]

我想取消透视该列并将 [GWP 2014] 拆分为两列 [GWP], [2014]。

输出应该是这样的

Region  [LOB]      [Metrics] [Year] [Value]
--------------------------------------------------
North   Workers    GWP       2013         38902.50 
North   Workers    GWP       2014    37,972,404
North   Workers    LR        2013            89
North   Workers    LR        2014            82

您能否建议如何做到这一点?

我是 SQL Server 中枢轴的新手

我还想每次使用动态列表将输出插入到一个新表中

【问题讨论】:

unpivot with dynamic columns plus column names的可能重复 你真的应该考虑修复你的数据库。至少年份应该是行中的一个值,也可能是 LR / GWP。 【参考方案1】:

您只需要在执行UNPIVOT 之后拆分列,如下所示:

WITH Unpivoted 
AS
(
    SELECT region, lob, columns, value
    FROM Regions
    UNPIVOT
    (
       columns
       FOR value IN([GWP 2013] , [GWP 2014] ,
                    [LR 2013]  , [LR 2014] ,
                    [GWP 2015], [LR 2015])
    ) AS u
) 
SELECT 
  region, 
  lob,
  columns,
  CAST(CASE WHEN value LIKE 'GWP%' THEN REPLACE(value,'GWP ', '')
       WHEN value LIKE 'LR%' THEN REPLACE(value,'LR ', '')
  END AS INT) AS Year,
  CASE WHEN value LIKE 'GWP%' THEN 'GWP'
       WHEN value LIKE 'LR%' THEN 'LR'
  END AS Metrics
FROM Unpivoted;

当然你应该动态地做它以避免手动列出列并动态地做它:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' +
                        QUOTENAME(column_name)
                      FROM information_schema.columns
                      WHERE table_name = 'Regions'
                        AND COLUMN_NAME <> 'Region' 
                        AND COLUMN_NAME <> 'LOB'
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');


SELECT @query = 'WITH Unpivoted 
                AS
                (
                    SELECT region, lob, columns, value
                    FROM Regions
                    UNPIVOT
                    (
                       columns
                       FOR value IN('+ @cols + ')
                    ) AS u
                ) 
                SELECT 
                  region, 
                  lob,
                  columns,
                  CAST(CASE WHEN value LIKE ''GWP%'' THEN REPLACE(value,''GWP '', '''')
                       WHEN value LIKE ''LR%'' THEN REPLACE(value,''LR '', '''')
                  END AS INT) AS Year,
                  CASE WHEN value LIKE ''GWP%'' THEN ''GWP''
                       WHEN value LIKE ''LR%'' THEN ''LR''
                  END AS Metrics
                FROM Unpivoted';


EXECUTE(@query);

这应该可以正常工作,假设:

[GWP 2013] , [GWP 2014] , [LR 2013] , [LR 2014] , [GWP 2015], [LR 2015], ... etc 的所有列都采用相同的格式(GWP 或 LR,然后是空格,然后是年份,以及

所有的列都是相同的数据类型int或十进制,如果数据类型不一样你应该在执行unpivot之前将它们全部转换为一种数据类型,否则你会得到一个错误。

SQL Fiddle Demo

这会给你:

| region |     lob |  columns | Year | Metrics |
|--------|---------|----------|------|---------|
|  North | Workers |  38902.5 | 2013 |     GWP |
|  North | Workers | 37972404 | 2014 |     GWP |
|  North | Workers |       70 | 2015 |     GWP |
|  North | Workers |       89 | 2013 |      LR |
|  North | Workers |       82 | 2014 |      LR |
|  North | Workers |       80 | 2015 |      LR |

更新:

我使用FOR XML PATH('') .. 将所有值列表连接到一个字符串中,这是 SQL Server 中的一种解决方法。 @cols 的值将是字符串:[GWP 2013], [GWP 2014], ...

如果您的字段数据类型不同,您必须在执行UNPVOT 之前对所有将在锚查询中取消透视的列进行强制转换,如下所示:

SELECT @query = 'WITH Unpivoted 
                AS
                (
                    SELECT region, lob, columns, value
                    FROM 
                    (
                       SELECT 
                         region,
                         lob,
                         CAST([GWP 2013] AS DECIMAL(10,2)) AS [GWP 2013],
                         CAST([GWP 2014] AS DECIMAL(10,2)) AS [GWP 2014],
                         ... etc
                       FROM Regions
                    ) AS t
                    UNPIVOT
                    (
                       columns
                       FOR value IN('+ @cols + ')
                    ) AS u
                ) 
                SELECT 
                  region, 
                  lob,
                  columns,
                  CAST(CASE WHEN value LIKE ''GWP%'' THEN REPLACE(value,''GWP '', '''')
                       WHEN value LIKE ''LR%'' THEN REPLACE(value,''LR '', '''')
                  END AS INT) AS Year,
                  CASE WHEN value LIKE ''GWP%'' THEN ''GWP''
                       WHEN value LIKE ''LR%'' THEN ''LR''
                  END AS Metrics
                FROM Unpivoted';

如果您发现手动为所有列编写强制转换很困难,您可以动态生成并附加它,例如:

DECLARE @colsCasted AS NVARCHAR(MAX);

select @colsCasted = STUFF((SELECT distinct ',' +
                        'CAST(' + QUOTENAME(column_name) + 'AS DECIMAL(10,2)) AS ' + QUOTENAME(column_name)
                      FROM information_schema.columns
                      WHERE table_name = 'Regions'
                        AND COLUMN_NAME <> 'Region' 
                        AND COLUMN_NAME <> 'LOB'
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

然后在动态查询中将该值附加到它:

SELECT @query = 'WITH Unpivoted 
                    AS
                    (
                        SELECT region, lob, columns, value
                        FROM 
                        (
                          SELECT region, lob,
                          ' + @colsCasted + '
                          FROM Regions
                        ) AS t
                        UNPIVOT
                        (
                           columns
                           FOR value IN('+ @cols + ')
                        ) AS u
                    ) 
                    SELECT 
                      region, 
                      lob,
                      columns,
                      CAST(CASE WHEN value LIKE ''GWP%'' THEN REPLACE(value,''GWP '', '''')
                           WHEN value LIKE ''LR%'' THEN REPLACE(value,''LR '', '''')
                      END AS INT) AS Year,
                      CASE WHEN value LIKE ''GWP%'' THEN ''GWP''
                           WHEN value LIKE ''LR%'' THEN ''LR''
                      END AS Metrics
                    FROM Unpivoted';


    EXECUTE(@query);

【讨论】:

这很有魅力。你能解释一下 xml_path 是如何工作的吗? 您还提到我需要转换,因为我的所有字段都不是 int 或 decimal,有些是 nvarchar。这种转换需要在单独的动态查询中完成吗?还是会成为@Query 的一部分? 这是什么意思 FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); @Mohit - 我更新了我的答案以澄清所有这些疑问,请在答案中查看我的更新。 感谢 Mahmoud 的澄清。非常感谢您的努力

以上是关于动态反透视和拆分列 SQL Server 2012的主要内容,如果未能解决你的问题,请参考以下文章

sql server 反透视表

SQL Server 中具有动态列的数据透视表

对具有不同类型的列使用动态反透视

如何在一个列上对 sql server 进行透视,但重命名为动态列

SQL Server 中的动态透视列

如何在不使用数据透视和反透视的情况下在 SQL Server 中水平显示数据?