实际表格大约有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
没有,这样做有没有其他的方法可以轻松完成。 –