我在我的SQL Server數據庫,看起來像一個預算表如下:SQL Server 2008的列到行數據
create table #cols (
acctid int,
yr int,
pd01 numeric(15,2),
pd02 numeric(15,2),
pd03 numeric(15,2),
.
.
.
pd13 numeric(15,2),
primary key(acctid, yr));
我需要在各列中的數據並將其切換到行基於格式表如下所示:
create table #rows (
acctid int,
yr int,
pd int,
amt numeric(15,2),
primary key(acctid, yr, pd));
現在我可以運行13條SQL語句是這樣的:
insert into #rows select acctid, yr, 1, pd01 from #cols;
.
.
.
insert into #rows select acctid, yr, 13, pd13 from #cols;
但我想知道是否有更高效/優雅的方式來做到這一點?
這正是我所期待的。我知道必須有一種優雅的方式來做到這一點。我甚至從來沒有聽說過UNPIVOT更不用說使用它了。謝謝。 – Caynadian 2013-04-25 16:57:52