SQL Server データベースに、次のような予算テーブルがあります。
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;
しかし、これを行うためのより効率的でエレガントな方法があるかどうか疑問に思っていましたか?