PIVOT 或 UNPIVOT:两者都行吗?
PIVOT or UNPIVOT: Will either work?
我四处寻找与我所拥有的特别相似的例子,但我找不到任何东西。据我所知,我不确定是否可行,但我想我会先在这里问一下。
1.原文Table
我有一个 table 看起来像这样:
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| Application | Year | YearTarg | JanTarg | FebTarg | MarTarg | AprTarg | MayTarg | JunTarg | JulTarg | AugTarg | SepTarg | OctTarg | NovTarg | DecTarg |
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| Mod | 2018 | 5700000 | 400000 | 300000 | 400000 | 600000 | 500000 | 500000 | 600000 | 500000 | 500000 | 600000 | 500000 | 400000 |
| RC | 2018 | 1700000 | 40000 | 50000 | 60000 | 80000 | 100000 | 120000 | 150000 | 180000 | 220000 | 250000 | 220000 | 230000 |
| Flow | 2018 | 2000000 | 30000 | 50000 | 20000 | 80000 | 250000 | 80000 | 70000 | 200000 | 300000 | 350000 | 200000 | 190000 |
| Non-RA | 2018 | 13400000 | 1100000 | 900000 | 1000000 | 1200000 | 1000000 | 1100000 | 1100000 | 1000000 | 1100000 | 1500000 | 1100000 | 1300000 |
| AMH | 2018 | 3500000 | 100000 | 100000 | 300000 | 200000 | 500000 | 400000 | 300000 | 500000 | 200000 | 200000 | 400000 | 300000 |
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
2。新建 Table
我希望返回的结果如下所示:
我真的很纠结,我认为部分原因是包含数字 1 代表 1 月的数字,2 代表 2 月的数字,依此类推。
我需要此功能才能根据月份值重新加入另一个 table,即 NewTable.Month = OtherTable.InvMonth (which is just MONTH(GETDATE()))
。
这可以做到吗?
Unpivot 会工作,但我认为 CROSS APPLY 更容易并且提供更多的灵活性。
例子
Select A.Application
,A.Year
,B.*
From YourTable A
Cross Apply ( values (1,JanTarg)
,(2,FebTarg)
,(3,MarTarg)
,(4,AprTarg)
,(5,MayTarg)
,(6,JunTarg)
,(7,JulTarg)
,(8,AugTarg)
,(9,SepTarg)
,(10,OctTarg)
,(11,NovTarg)
,(12,DecTarg)
) B ([Month],[Target])
如果您更喜欢使用UNPIVOT
:
declare @tmp table([Application] varchar(50), [Year] int, [YearTarg] int, [JanTarg] int, [FebTarg] int, [MarTarg] int, [AprTarg] int, [MayTarg] int, [JunTarg] int, [JulTarg] int, [AugTarg] int, [SepTarg] int, [OctTarg] int, [NovTarg] int, [DecTarg] int)
insert into @tmp values
('Mod' , 2018, 5700000, 400000, 300000, 400000, 600000, 500000, 500000, 600000, 500000, 500000, 600000, 500000, 400000),
('RC' , 2018, 1700000, 40000, 50000, 60000, 80000, 100000, 120000, 150000, 180000, 220000, 250000, 220000, 230000),
('Flow' , 2018, 2000000, 30000, 50000, 20000, 80000, 250000, 80000, 70000, 200000, 300000, 350000, 200000, 190000),
('Non-RA', 2018,13400000,1100000, 900000,1000000,1200000,1000000,1100000,1100000,1000000,1100000,1500000,1100000,1300000),
('AMH' , 2018, 3500000, 100000, 100000, 300000, 200000, 500000, 400000, 300000, 500000, 200000, 200000, 400000, 300000)
select u.[Application],
u.[Year],
case when u.[Month] ='JanTarg' then 1 when u.[Month] ='FebTarg' then 2 when u.[Month] ='MarTarg' then 3 when u.[Month] ='AprTarg' then 4 when u.[Month] ='MayTarg' then 5 when u.[Month] ='JunTarg' then 6 when u.[Month] ='JulTarg' then 7 when u.[Month] ='AugTarg' then 8 when u.[Month] ='SepTarg' then 9 when u.[Month] ='OctTarg' then 10 when u.[Month] ='NovTarg' then 11 when u.[Month] ='DecTarg' then 12 end as [Month],
u.[Target]
from @tmp s
unpivot
(
[Target]
for [Month] in ([JanTarg],[FebTarg],[MarTarg],[AprTarg],[MayTarg],[JunTarg],[JulTarg],[AugTarg],[SepTarg],[OctTarg],[NovTarg],[DecTarg])
) u;
结果:
我四处寻找与我所拥有的特别相似的例子,但我找不到任何东西。据我所知,我不确定是否可行,但我想我会先在这里问一下。
1.原文Table
我有一个 table 看起来像这样:
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| Application | Year | YearTarg | JanTarg | FebTarg | MarTarg | AprTarg | MayTarg | JunTarg | JulTarg | AugTarg | SepTarg | OctTarg | NovTarg | DecTarg |
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| Mod | 2018 | 5700000 | 400000 | 300000 | 400000 | 600000 | 500000 | 500000 | 600000 | 500000 | 500000 | 600000 | 500000 | 400000 |
| RC | 2018 | 1700000 | 40000 | 50000 | 60000 | 80000 | 100000 | 120000 | 150000 | 180000 | 220000 | 250000 | 220000 | 230000 |
| Flow | 2018 | 2000000 | 30000 | 50000 | 20000 | 80000 | 250000 | 80000 | 70000 | 200000 | 300000 | 350000 | 200000 | 190000 |
| Non-RA | 2018 | 13400000 | 1100000 | 900000 | 1000000 | 1200000 | 1000000 | 1100000 | 1100000 | 1000000 | 1100000 | 1500000 | 1100000 | 1300000 |
| AMH | 2018 | 3500000 | 100000 | 100000 | 300000 | 200000 | 500000 | 400000 | 300000 | 500000 | 200000 | 200000 | 400000 | 300000 |
+-------------+------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
2。新建 Table
我希望返回的结果如下所示:
我真的很纠结,我认为部分原因是包含数字 1 代表 1 月的数字,2 代表 2 月的数字,依此类推。
我需要此功能才能根据月份值重新加入另一个 table,即 NewTable.Month = OtherTable.InvMonth (which is just MONTH(GETDATE()))
。
这可以做到吗?
Unpivot 会工作,但我认为 CROSS APPLY 更容易并且提供更多的灵活性。
例子
Select A.Application
,A.Year
,B.*
From YourTable A
Cross Apply ( values (1,JanTarg)
,(2,FebTarg)
,(3,MarTarg)
,(4,AprTarg)
,(5,MayTarg)
,(6,JunTarg)
,(7,JulTarg)
,(8,AugTarg)
,(9,SepTarg)
,(10,OctTarg)
,(11,NovTarg)
,(12,DecTarg)
) B ([Month],[Target])
如果您更喜欢使用UNPIVOT
:
declare @tmp table([Application] varchar(50), [Year] int, [YearTarg] int, [JanTarg] int, [FebTarg] int, [MarTarg] int, [AprTarg] int, [MayTarg] int, [JunTarg] int, [JulTarg] int, [AugTarg] int, [SepTarg] int, [OctTarg] int, [NovTarg] int, [DecTarg] int)
insert into @tmp values
('Mod' , 2018, 5700000, 400000, 300000, 400000, 600000, 500000, 500000, 600000, 500000, 500000, 600000, 500000, 400000),
('RC' , 2018, 1700000, 40000, 50000, 60000, 80000, 100000, 120000, 150000, 180000, 220000, 250000, 220000, 230000),
('Flow' , 2018, 2000000, 30000, 50000, 20000, 80000, 250000, 80000, 70000, 200000, 300000, 350000, 200000, 190000),
('Non-RA', 2018,13400000,1100000, 900000,1000000,1200000,1000000,1100000,1100000,1000000,1100000,1500000,1100000,1300000),
('AMH' , 2018, 3500000, 100000, 100000, 300000, 200000, 500000, 400000, 300000, 500000, 200000, 200000, 400000, 300000)
select u.[Application],
u.[Year],
case when u.[Month] ='JanTarg' then 1 when u.[Month] ='FebTarg' then 2 when u.[Month] ='MarTarg' then 3 when u.[Month] ='AprTarg' then 4 when u.[Month] ='MayTarg' then 5 when u.[Month] ='JunTarg' then 6 when u.[Month] ='JulTarg' then 7 when u.[Month] ='AugTarg' then 8 when u.[Month] ='SepTarg' then 9 when u.[Month] ='OctTarg' then 10 when u.[Month] ='NovTarg' then 11 when u.[Month] ='DecTarg' then 12 end as [Month],
u.[Target]
from @tmp s
unpivot
(
[Target]
for [Month] in ([JanTarg],[FebTarg],[MarTarg],[AprTarg],[MayTarg],[JunTarg],[JulTarg],[AugTarg],[SepTarg],[OctTarg],[NovTarg],[DecTarg])
) u;
结果: