PIVOT并复制表的某些列

时间:2016-10-12 07:07:53

标签: sql sql-server tsql sql-server-2012 pivot

我有一个> as.integer(root.of.2.sqrd)==2 [1] TRUE 表:

FormSummary

如何转动formID formName Version ReleaseDate Location ======================================================== AA-01 PAYMENTS 1.3 1/1/2012 CA AA-02 LOANS 1.4 2/1/2012 CA AA-03 ADVANCES 1.6 3/20/2012 CA AA-01 PAYMENTS 1.2 12/1/2011 US AA-02 LOANS 1.4 2/1/2012 US AA-03 ADVANCES 1.6 3/20/2012 US AA-01 PAYMENTS 1.3 12/1/2012 PH AA-02 LOANS 1.4 2/1/2012 PH AA-03 ADVANCES 1.5 2/14/2012 PH 列,以便我能够提供以下输出:

location

创建表格的脚本:

formID    formName    Version (CA)    ReleaseDate (CA)    Version (US)    ReleaseDate (US)    Version (PH)    ReleaseDate (PH)
==============================================================================================================================
AA-01     PAYMENTS    1.3             1/1/2012            1.2             12/1/2011           1.3             12/1/2012
AA-02     LOANS       1.4             2/1/2012            1.4             2/1/2012            1.4             2/1/2012
AA-03     ADVANCES    1.6             3/20/2012           1.6             3/20/2012           1.5             2/14/2012

1 个答案:

答案 0 :(得分:2)

首先使用sys.columns表来获取列名。然后CROSS用Location s加入它们以获得像

这样的列名
,[Version (CA)],[ReleaseDate (CA)],[Version (PH)],[ReleaseDate (PH)],[Version (US)],[ReleaseDate (US)]

然后使用UNPIVOT使表格看起来像这样:

formID  formName    Columns             Values
AA-01   PAYMENTS    Version (CA)        1.3
AA-01   PAYMENTS    ReleaseDate (CA)    2012-01-01
AA-02   LOANS       Version (CA)        1.4
...
AA-03   ADVANCES    Version (PH)        1.5
AA-03   ADVANCES    ReleaseDate (PH)    2012-02-14

然后使用PIVOT,将列名存储在@col

此脚本将为您提供所需内容:

DECLARE @sql nvarchar(max),
        @col nvarchar(max)

SELECT @col = (
    SELECT ',' + QUOTENAME([name]+ ' ('+[Location]+')')
    FROM sys.columns c
    CROSS JOIN (
        SELECT DISTINCT [Location] 
        FROM #FormSummary
        ) as f
    WHERE c.[object_id] = OBJECT_ID(N'#FormSummary') and c.column_id > 2 and [name] != 'Location'
    ORDER BY [Location]
    FOR XML PATH('')
)

SELECT @sql = N'
SELECT *
FROM (
    SELECT  formID, 
            formName,
            [Columns] + '' (''+[Location]+'')'' as [Columns],
            [Values]
    FROM (
        SELECT  formID, 
                formName, 
                CAST([Version] as nvarchar(max)) [Version], 
                CAST(ReleaseDate as nvarchar(max)) ReleaseDate, 
                CAST([Location] as nvarchar(max)) [Location]
        FROM #FormSummary
    ) as t
    UNPIVOT (
        [Values] FOR [Columns] IN ([Version], ReleaseDate)
    ) as unpvt
) as d
PIVOT (
    MAX([Values]) FOR [Columns] IN ('+STUFF(@col,1,1,'')+')
) as pvt'

EXEC sp_executesql @sql

输出:

formID  formName    Version (CA)    ReleaseDate (CA)    Version (PH)    ReleaseDate (PH)    Version (US)    ReleaseDate (US)
AA-03   ADVANCES    1.6             2012-03-20          1.5             2012-02-14          1.6             2012-03-20
AA-02   LOANS       1.4             2012-02-01          1.4             2012-02-01          1.4             2012-02-01
AA-01   PAYMENTS    1.3             2012-01-01          1.3             2012-12-01          1.2             2011-12-01