我想知道是否有人可以提供帮助:我想将行转换为列。这是原始表:
我尝试过使用数据透视,但是这种情况对我来说太复杂了。
declare @Table AS TABLE
(
TYPE VARCHAR(100) ,
SERIE VARCHAR(100) ,
CUR1 INT,
CUR2 INT
)
INSERT @Table
( TYPE, SERIE, CUR1, CUR2)
VALUES
( 'CORP', 'S1' ,2122,322 ),
( 'CORP', 'S2' ,321,546 ),
( 'SER', 'S1',543,788 ),
( 'SER', 'S2' ,655, 988 )
我希望输出类似于附件表:
答案 0 :(得分:0)
请尝试此操作,此方法的变体将有所帮助:-
;with cte as (
select SERIE, [CORP] as [CORP_CUR1], [SER] as [SER_CUR1] from (
select type , serie, cur1 from @Table)
as d
pivot
( max(cur1) for [type] in ( [CORP], [SER]) ) as p
),
ct as (
select SERIE, [CORP] as [CORP_CUR2], [SER] as [SER_CUR2] from (
select type , serie, cur2 from @Table)
as d
pivot
( max(cur2) for [type] in ( [CORP], [SER]) ) as p
)
select cte.SERIE, cte.[CORP_CUR1], cte.[SER_CUR1], ct.[CORP_CUR2], ct.[SER_CUR2] from cte inner join ct on cte.SERIE=ct.SERIE