實際表格大約有600萬行,每行中的num1和num2表示數字邊界。我需要將兩列集合轉換爲單列,並將之應用於先前共享單行的兩行中的每一行。下面是一個小樣本。我看着Unpivot的例子,但似乎沒有什麼符合我的需要。任何人都可以推薦正確的方式去?我意識到我最終會得到1200萬行。SQL Server 2012摺疊/取消兩個列合併成一個?
謝謝。
declare @orig table (num1 bigint , num2 bigint , metakey tinyint)
insert into @orig
select 7216,7471 , 0
union all
select 7472,8239 , 1
union all
select 8240,9263 , 2
union all
select 9264,11311 , 3
declare @dest table (allnum bigint , metakey tinyint)
-- Wanted result set:
/*
select * from @dest
7216 0
7471 0
7472 1
8239 1
8240 2
9263 2
9264 3
11311 3
*/
我明白,這適用於我的表變量樣本,但對於真正的大集不聞右:
insert into @dest
select num1 , metakey
from @orig
union all
select num2 , metakey
from @orig
order by 1
沒有,這樣做有沒有其他的方法可以輕鬆完成。 –