如何将两个表结构合成一个表结构?并提取数据

pcgreen 2004-10-18 04:06:41
望高手们指点迷津,或是有更好的数据结构代替我的方法
现有表
tb1
id name
11 ab
22 cd
33 ef
44 gh

tb2
BasicId fd1 fd2 fd3

现在是想以表tb2为基础,以表tb2的字段为基本字段,以表一tb1字段name的数据为扩展字段,合并成新表,即
tb3
BasicId fd1 fd2 fd3 ab cd ef gh


提取数据的要求:
源表还是tb2
tb2
BasicId fd1 fd2 fd3
1 a1 a2 a3
2 b1 b2 b3
3 c1 c2 c3
扩展表
tb4
OtherId name value
1 ab 11
1 cd 22
2 ab 11
2 ef 33
2 gh 44
3 cd 22
从这两个表中(通过BasicId=OtherId关联)提取数据到tb3中,最终结果为:
tb3
BasicId fd1 fd2 fd3 ab cd ef gh
1 a1 a2 a3 11 22
2 b1 b2 b3 11 33 44
3 c1 c2 c3 22
...全文
313 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-10-18
  • 打赏
  • 举报
回复
如果是用插入记录来保持更新状态的话,不如用我上面那个生成tb3的

因为要用插入记录保持更新的话,要先删除旧记录,这也得花时间
pcgreen 2004-10-18
  • 打赏
  • 举报
回复
谢谢大家!!
pcgreen 2004-10-18
  • 打赏
  • 举报
回复
tb1的记录变了就没办法了,只能重新生成tb3,但如果没改变的话,我可以改成插入记录来保持数据的最新状态.
zjcxc 元老 2004-10-18
  • 打赏
  • 举报
回复
--如果你不需要保证取得的数据是最新的,只是自己根据需要决定是否重建tb3,则可以用下面的来生成tb3,以后要查询的时候,如果觉得没有必要重新取数,则直接查询tb3

--生成tb3的合并处理
if objectproperty(object_id('tb3'),'isusertable') is not null
drop table tb3

declare @s varchar(8000)
set @s=''
select @s=@s+',['+name+']=max(case b.name when '''+name+''' then cast(b.value as varchar(10)) else '''' end)'
from tb1
exec('
select a.*'+@s+'
into tb3 from tb2 a,tb4 b
where a.BasicId=b.OtherId
group by a.BasicId,a.fd1,a.fd2,a.fd3')

select * from tb3
zjcxc 元老 2004-10-18
  • 打赏
  • 举报
回复
效果是差不多的

其一,你做固定表的话,tb1的记录变化了,势必修改表结构
其一,为了保持最新数据,你一样要从tb1,tb2,tb4中重新统计数据吧?
pcgreen 2004-10-18
  • 打赏
  • 举报
回复
to:邹大哥
能不能先生成一个固定表,再插入数据啊,因为源数据较多,如果每次都动态生成速度就较慢。我想生成一个过度表tb3,以后还要在tb3上做报表,如果能生成过度表,那我以后报表速度就快了
newdongkui 2004-10-18
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
declare @sql2 varchar(8000)
select @sql='',@sql2 = 'select tb2.* '
set @sql='select OtherId'
select @sql=@sql+',sum(case name when '''+name+''' then value end)['+name+']'
from (select distinct name from tb4)a
set @sql=@sql+' from tb4 group by OtherId'

select @sql2 = @sql2 + ',b.' + b.name
from (select distinct name from tb4) as b

select @sql2 = @sql2 + ' from tb2,('+@sql+') as b where tb2.basicid = b.otherid '
--如果需要直接生成可以这样
--select @sql2 = @sql2 + ' from tb2,('+@sql+') as b into tb3 where tb2.basicid = b.otherid '
exec(@sql2)
pcgreen 2004-10-18
  • 打赏
  • 举报
回复
to :chinaandys
测试过不行
create table tb1
(
id int,
name varchar(10)
)
insert into tb1 values(11,'ab')
insert into tb1 values(22,'cd')
insert into tb1 values(33,'ef')
insert into tb1 values(44,'gh')

create table tb2
(
BasicId int,
fd1 varchar(10),
fd2 varchar(10),
fd3 varchar(10)
)
insert into tb2 values(1,'a1','a2','a3')
insert into tb2 values(2,'b1','b2','b3')
insert into tb2 values(3,'c1','c2','c3')

create table tb4
(
OtherId int,
name varchar(10),
value varchar(10)
)

insert into tb4 values(1, 'ab','11')
insert into tb4 values(1, 'cd','22')
insert into tb4 values(2, 'ab','11')
insert into tb4 values(2, 'ef','33')
insert into tb4 values(2, 'gh','44')
insert into tb4 values(3, 'cd','22')
selectplayer 2004-10-18
  • 打赏
  • 举报
回复
select tb2.*,tb5.*
from tb2,(select OtherId,ab=max(case when name='ab' then value else null end),
cd=max(case when name='cd' then value else null end),
ef=max(case when name='ef' then value else null end),
gh=max(case when name='gh' then value else null end)
from tb4
group by OtherId) tb5
where tb2.BasicId=tb5.OtherId
zjcxc 元老 2004-10-18
  • 打赏
  • 举报
回复
--示例

--示例数据
create table tb1(id int,name varchar(10))
insert tb1 select 11,'ab'
union all select 22,'cd'
union all select 33,'ef'
union all select 44,'gh'

create table tb2(BasicId int,fd1 varchar(10),fd2 varchar(10),fd3 varchar(10))
insert tb2 select 1,'a1','a2','a3'
union all select 2,'b1','b2','b3'
union all select 3,'c1','c2','c3'

create table tb4(OtherId int,name varchar(10),value int)
insert tb4 select 1,'ab',11
union all select 1,'cd',22
union all select 2,'ab',11
union all select 2,'ef',33
union all select 2,'gh',44
union all select 3,'cd',22
go

--合并处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+name+']=max(case b.name when '''+name+''' then cast(b.value as varchar(10)) else '''' end)'
from tb1
exec('
select a.*'+@s+'
from tb2 a,tb4 b
where a.BasicId=b.OtherId
group by a.BasicId,a.fd1,a.fd2,a.fd3')
go

--删除测试
drop table tb1,tb2,tb4


/*--结果:

BasicId fd1 fd2 fd3 ab cd ef gh
----------- ---------- ---------- ---------- ---- ---- ---- ----
1 a1 a2 a3 11 22
2 b1 b2 b3 11 33 44
3 c1 c2 c3 22
--*/
chinaandys 2004-10-18
  • 打赏
  • 举报
回复
1 11 22 11 NULL NULL 22
2 11 NULL 11 33 44 NULL
3 NULL 22 NULL NULL NULL 22
chinaandys 2004-10-18
  • 打赏
  • 举报
回复
declare @sql varchar(3000)
set @sql=''
set @sql='select OtherId'
select @sql=@sql+',sum(case name when '''+name+''' then value end)['+name+']'
from (select name from tb4)a
set @sql=@sql+' from tb4 group by OtherId'
exec(@sql)
netpurk 2004-10-18
  • 打赏
  • 举报
回复
很复杂啊。还是将所有的都按照动态生成表结构来做的比较好啊。
采用视图的方式进行管理啊。
只是建议。

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧