转置表 - Pivot SQL Server 2016

时间:2017-10-23 13:29:10

标签: sql sql-server pivot transpose

我有一个看起来像这样的表: SourceTable

CREATE TABLE [Temp].[tblMyleneTest]
(
    [DispOrder] [INT] NULL,
    [ReferencePeriod] [VARCHAR](7) NULL,
    [TypeOfGSUF] [VARCHAR](8) NULL,
    [FactAllocReferencePeriod] [VARCHAR](4) NULL,
    [Variable] [VARCHAR](50) NULL,
    [Value] [INT] NULL 
) ON [PRIMARY]


INSERT INTO [Temp].[tblMyleneTest]
VALUES (1, '201710F', 'Reduced', '2015', 'OperatingEntityNumberCount', '13540688'),
       (2, '201710F', 'Reduced', '2015', 'EnterpriseCodeCount', '6041138'),
       (3, '201710F', 'Reduced', '2015', 'EstablishmentCodeCount', '6554081'),
       (4, '201710F', 'Reduced', '2015', 'LocationCodeCount', '6600130'),
       (5, '201710F', 'Reduced', '2015', 'NAICSNotNullCount', '9943634'),
       (6, '201710F', 'Reduced', '2015', 'ReportingEntityNumbeCount', '2608'),
       (7, '201710F', 'Reduced', '2015', 'LocationCode_NotEverAliveCount', '2339'),
       (8, '201710F', 'Reduced', '2015', 'OperatingEntityNumberCountFF', '3078'),
       (1, '201706F', 'Reduced', '2014', 'OperatingEntityNumberCount', '13557946'),
       (2, '201706F', 'Reduced', '2014', 'EnterpriseCodeCount', '6046857'),
       (3, '201706F', 'Reduced', '2014', 'EstablishmentCodeCount', '6560136'),
       (4, '201706F', 'Reduced', '2014', 'LocationCodeCount', '6606226'),
       (8, '201706F', 'Reduced', '2014', 'OperatingEntityNumberCountFF', '3078')

我希望对ReferencePeriodFactAllocReferencePeriodTypeOfGSUF进行分组。如您所见,对于相同的变量名称有2条记录。在ReferencePeriod 201710F上,另一个在201706F上。我希望这些计数能够并存。我想要这样的东西:

Result Table

我可以转动表格以启动ReferencePeriod。但不是相关的FactAllocReferencePeriod / TypeOfGSUF ......

SELECT * 
FROM [Temp].[tblMyleneTest]
PIVOT (MAX(value) FOR ReferencePeriod IN ([201710F], [201706F])) p

你能帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:0)

引用this answer on pivoting rows into columns

SELECT
    [Variable], piv.[201706F], piv.[201710F]
FROM 
(
    select [DispOrder], [Variable], [ReferencePeriod], [Value] from  #tblMyleneTest
) src
PIVOT (
    MAX([Value])
    FOR ReferencePeriod IN ([201710F],[201706F])
) piv

按[DispOrder]

排序

答案 1 :(得分:0)

我会使用条件聚合代替pivot。语法对我来说不那么愚蠢。

select variable
    , R201710f = max(case when ReferencePeriod = '201710F' then Value end)
    , F201706f = max(case when ReferencePeriod = '201706F' then Value end)
from tblMyleneTest
group by variable
order by variable

答案 2 :(得分:0)

result

我正在丢失TypeOfGSUF和FactAllocReferencePeriod ......

答案 3 :(得分:0)

这是我的最后一段代码:

    -- Transpose tblSUFGenericCountsReportYYYYMM to get proper display --
SET @p_SQLCMD  = N' DROP TABLE IF EXISTS ' + @p_SUFReportTable + '' 
               + N' SELECT [DispOrder], [Variable], piv.[' + @p_ReferencePeriod2 + '], piv.[' + @p_ReferencePeriod1 + '] '
               + N' INTO ' + @p_SUFReportTable + ' from ( '
               + N' SELECT [DispOrder], [Variable], [ReferencePeriod], [Value] FROM  Temp.tblSUFGenericCountsReport ) src '
               + N' PIVOT ( MAX([Value]) FOR ReferencePeriod IN ([' + @p_ReferencePeriod1 + '],[' + @p_ReferencePeriod2 + ']) ) piv'
               + N' ORDER BY [DispOrder];'

EXEC sp_executesql @statement = @p_SQLCMD

然后添加缺少的值:

-- Insert the TypeOfGSUF and FactorAllocationReferenceYear values to the report --
SET @p_SQLCMD  = N' INSERT INTO '+ @p_SUFReportTable +' (DispOrder, variable, "'+ @p_ReferencePeriod2 +'", "'+ @p_ReferencePeriod1 +'", Flag)'
               + N' VALUES (0 ,''TYPE OF GSUF'', "'+ @p_SUFMonthReduced +'", "'+ @p_SUFMonthReduced +'", Null),'
               + N' (1 ,''T4 RY used for allocation view'', ' + @p_PrevFactAllocReferencePeriod + ', ' + @p_FactAllocReferencePeriod + ', Null) ;'

PRINT @p_SQLCMD
EXEC sp_executesql @statement = @p_SQLCMD