如何动态地将行转换为列,并为每列使用不同的列名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何动态地将行转换为列,并为每列使用不同的列名相关的知识,希望对你有一定的参考价值。

如何将行转换为列并为每列创建不同的名称?

create table #TempTable  (InvoiceNum int,State varchar(2), ChargeName varchar(50),  PercentageRate decimal(5,3), FlatRate decimal(5,2))
insert into #TempTable values   (235736, 'AZ','Inspection & Policy Fee',  NULL,250.00)
                                ,(235736, 'AZ','Surplus Line Tax',0.03,NULL)
                                ,(235736, 'AZ','Stamping Fee',0.002,NULL

)

我需要这样的东西:

enter image description here

更新:

使用示例我能够将其取消,但结果不是我想要的:

create table #TempTable  (InvoiceNum int,State varchar(2), ChargeName varchar(50),  PercentageRate decimal(5,3), FlatRate decimal(5,2))
insert into #TempTable values   (235736, 'AZ','Inspection & Policy Fee',  NULL,250.00)
                                ,(235736, 'AZ','Surplus Line Tax',0.03,NULL)
                                ,(235736, 'AZ','Stamping Fee',0.002,NULL)

--select * from @TempTable


Declare @SQL nvarchar(max),
        @query nvarchar(max)

select @SQL = STUFF((SELECT ',' + QUOTENAME(ChargeName) 
                    from #TempTable
                    group by ChargeName, InvoiceNum
                    order by InvoiceNum
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
--select @SQL
set   @SQL = 'SELECT ' + @SQL + '  from
        (
            select PercentageRate, ChargeName
            from #TempTable
        ) x
        pivot
        (
        max(PercentageRate)
        for ChargeName in (' + @SQL + ')
        ) p '

exec sp_executesql @SQL;

enter image description here

更新:

在查询下运行给我这个:

enter image description here

为什么ChargeName不在第一行?我希望看到这样:我错过了什么?

enter image description here

declare  @TempTable table  (InvoiceNum int,StateID varchar(2), ChargeName varchar(50),  PercentageRate decimal(5,3), FlatRate decimal(5,2))
insert into @TempTable values   (235736, 'AZ','Inspection & Policy Fee',  NULL,250.00)
                                ,(235736, 'AZ','Surplus Line Tax',0.03,NULL)
                                ,(235736, 'AZ','Stamping Fee',0.002,NULL)
select 
        InvoiceNum,
        ChargeName,
        StateID,
        PercentageRate,
        FlatRate,
        row_number() over (partition by InvoiceNum order by ChargeName) as RN
into #TempTable
 from @TempTable #TempTable

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(RN)
FROM (SELECT DISTINCT RN FROM #TempTable) AS RN

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT InvoiceNum,  ' + @ColumnName + '
    FROM #TempTable
    PIVOT(MAX(ChargeName) 
          FOR RN IN (' + @ColumnName + ')) AS PVTTable'

EXEC sp_executesql @DynamicPivotQuery
drop table #TempTable
答案

我会根据需要多次加入临时表。鉴于你的#TempTable

SELECT T1.InvoiceNum,
    Tax1_Jurisdiction = T1.State, Tax1_TaxType = T1.ChargeName, Tax1_Percent = T1.PercentageRate, Tax1_FixedRate = T1.FlatRate, 
    Tax2_Jurisdiction = T2.State, Tax2_TaxType = T2.ChargeName, Tax2_Percent = T2.PercentageRate, Tax2_FixedRate = T2.FlatRate, 
    Tax3_Jurisdiction = T3.State, Tax3_TaxType = T3.ChargeName, Tax3_Percent = T3.PercentageRate, Tax3_FixedRate = T3.FlatRate
FROM #TempTable T1
JOIN #TempTable T2 ON T1.InvoiceNum = T2.InvoiceNum
JOIN #TempTable T3 ON T1.InvoiceNum = T3.InvoiceNum
WHERE T1.ChargeName = 'Inspection & Policy Fee'
    AND T2.ChargeName = 'Surplus Line Tax'
    AND T3.ChargeName = 'Stamping Fee'
;

以上是关于如何动态地将行转换为列,并为每列使用不同的列名的主要内容,如果未能解决你的问题,请参考以下文章

在 SQL Server 视图中有效地将行转换为列

使用R将行转换为列,将列转换为行

在 Oracle 中动态地将行转为列

如何在 SQL Server 中将行动态转换为列

Mysql查询根据两列动态将行转换为列

使用可能的组合将行转换为列