现有一个表:
表A
adress pepole
宁波 张三(NB)
宁波 李四(NB)
宁波 王五(NB)
杭州 张三(HZ)
杭州 李四(HZ)
杭州 王五(HZ)
转化为表
表B
NBpepole HZpepole
张三(NB) 张三(HZ)
李四(NB) 李四(HZ)
王五(NB) 王五(HZ)
方法一:
create table T(adress nvarchar(10),pepole nvarchar(10))
insert T select '宁波', '张三(NB)'
union all select '宁波', '李四(NB)'
union all select '宁波', '王五(NB)'
union all select '杭州', '张三(HZ)'
union all select '杭州', '李四(HZ)'
union all select '杭州', '王五(HZ)'
select ID=identity(int,1,1),* into #T1 from T where adress='宁波'
select ID=identity(int,1,1),* into #T2 from T where adress='杭州'
insert B(NBpepole,HZpepole)
select #T1.pepole,#T2.pepole
from #T1
full join #T2 on #T1.ID=#T2.ID
方法二:
select NBpepole=nb.pepole, HZpepole=hz.pepole into B from
(select pepole from a where adress='宁波') nb full join
(select pepole from a where adress='杭州') hz
on left(nb.pepole,charindex('(',nb.pepole)-1)=left(hz.pepole,charindex('(',hz.pepole)-1)
本文介绍了一种将原始表结构通过两种不同方法转换为新表结构的技术。方法一使用了临时表和FULL JOIN来实现,而方法二则利用了FULL JOIN结合子查询的方式完成转换。这些技巧对于进行复杂的数据处理和报表生成非常有用。

被折叠的 条评论
为什么被折叠?



