根据旧的表创建新表!急!

zzit0721 2005-10-12 04:04:02
原两表为
表名:a
表结构如下(A1,2,3分别代表系统1,2,3在不同日期的值):
日期 A1 A2 A3
2005-10-1 1 1.3 .14
2005-10-2 3 2.2 2.1
2005-10-3 2 2.2 2.1
2005-10-4 3.2 3.1 3.1
2005-10-5 3.2 3 2
2005-10-6 2 2 2.1
2005-10-7 3 4 5
表名:b
表结构如下
表名称 表ID
系统1 A1
系统2 A2
系统3 A3

我现在想得到的新表结构如下:
表名称 值 日期 表ID
系统1 1 2005-10-1 A1
系统1 3 2005-10-2 A1
系统1 2 2005-10-3 A1
.........
........
.........
系统3 2 2005-10-6 A3
系统3 3 2005-10-7 A3

请问如何写这个SQL,进行创新表并且把值插入新表



...全文
159 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzit0721 2005-10-12
  • 打赏
  • 举报
回复
对的,我要的就是用你的游标解决的那个,谢谢
子陌红尘 2005-10-12
  • 打赏
  • 举报
回复
楼上的SQL处理用了游标和循环,适用于A表列数过多和列数不固定的情况,楼主试试。
samfeng_2003 2005-10-12
  • 打赏
  • 举报
回复
好象用cross join集合就可以得到你的答案了,你说你的列多,那么也没有关系啊,你毕竟只需要其中的几列就可以了啊。我觉得可能你没有把你的要求说清楚。 libin_ftsafe老大的才是你要求表达的结果吧!
子陌红尘 2005-10-12
  • 打赏
  • 举报
回复
--生成测试数据
create table a(日期 datetime,A1 numeric(5,2),A2 numeric(5,2),A3 numeric(5,2))
insert into a select '2005-10-1',1 ,1.3,0.14
insert into a select '2005-10-2',3 ,2.2,2.1
insert into a select '2005-10-3',2 ,2.2,2.1
insert into a select '2005-10-4',3.2,3.1,3.1
insert into a select '2005-10-5',3.2,3 ,2
insert into a select '2005-10-6',2 ,2 ,2.1
insert into a select '2005-10-7',3 ,4 ,5
create table b(表名称 varchar(20),表ID varchar(20))
insert into b select '系统1','A1'
insert into b select '系统2','A2'
insert into b select '系统3','A3'

create table c(表名称 varchar(20),值 numeric(5,2),日期 datetime,表ID varchar(20))

declare @name varchar(20)
declare c_cursor cursor for
select name from syscolumns where id=object_id('a') and name!=N'日期'

open c_cursor

fetch next from c_cursor into @name

while @@fetch_status=0
begin
exec('insert into C select b.表名称,值=a.'+@name+',a.日期,b.表ID from a,b where b.表ID='''+@name+'''')
fetch next from c_cursor into @name
end

close c_cursor
deallocate c_cursor


--查看结果
select * from C


--输出结果
/*
表名称 值 日期 表ID
------- ------ ------------------------- ----
系统1 1.00 2005-10-01 00:00:00.000 A1
系统1 3.00 2005-10-02 00:00:00.000 A1
系统1 2.00 2005-10-03 00:00:00.000 A1
系统1 3.20 2005-10-04 00:00:00.000 A1
系统1 3.20 2005-10-05 00:00:00.000 A1
系统1 2.00 2005-10-06 00:00:00.000 A1
系统1 3.00 2005-10-07 00:00:00.000 A1
系统2 1.30 2005-10-01 00:00:00.000 A2
系统2 2.20 2005-10-02 00:00:00.000 A2
系统2 2.20 2005-10-03 00:00:00.000 A2
系统2 3.10 2005-10-04 00:00:00.000 A2
系统2 3.00 2005-10-05 00:00:00.000 A2
系统2 2.00 2005-10-06 00:00:00.000 A2
系统2 4.00 2005-10-07 00:00:00.000 A2
系统3 0.14 2005-10-01 00:00:00.000 A3
系统3 2.10 2005-10-02 00:00:00.000 A3
系统3 2.10 2005-10-03 00:00:00.000 A3
系统3 3.10 2005-10-04 00:00:00.000 A3
系统3 2.00 2005-10-05 00:00:00.000 A3
系统3 2.10 2005-10-06 00:00:00.000 A3
系统3 5.00 2005-10-07 00:00:00.000 A3
*/

--删除测试数据
drop table a,b,c
samfeng_2003 2005-10-12
  • 打赏
  • 举报
回复
但是我加完数据看,好象又是对的,可以去掉[create],[union]调试一下!
[create] table a
(日期 varchar(20),a1 varchar(20),a2 varchar(20),a3 varchar(20))
insert a
select '2005-10-1','1','1.3','.14' [union] all
select '2005-10-2','3','2.2','2.1' [union] all
select '2005-10-3','2','2.2','2.1' [union] all
select '2005-10-4','3.2','3.1','3.1' [union] all
select '2005-10-5','3.2','3' ,'2' [union] all
select '2005-10-6','2' ,'2' ,'2.1' [union] all
select '2005-10-7','3' ,'4' ,'5'

[create] table b
(col1 varchar(20),col2 varchar(20))

insert b
select '系统1','a1' [union] all
select '系统2','a2' [union] all
select '系统3','a3'

select col1 as 表名称,a.a1 as 值,a.日期 as 日期,col2 as 表id into # from a,b order by b.col1
select * from #
drop table #
drop table b
drop table a

表名称 值 日期 表id
-------------------- -------------------- -------------------- --------------------
系统1 1 2005-10-1 a1
系统1 3 2005-10-2 a1
系统1 2 2005-10-3 a1
系统1 3.2 2005-10-4 a1
系统1 3.2 2005-10-5 a1
系统1 2 2005-10-6 a1
系统1 3 2005-10-7 a1
系统2 1 2005-10-1 a2
系统2 3 2005-10-2 a2
系统2 2 2005-10-3 a2
系统2 3.2 2005-10-4 a2
系统2 3.2 2005-10-5 a2
系统2 2 2005-10-6 a2
系统2 3 2005-10-7 a2
系统3 1 2005-10-1 a3
系统3 3 2005-10-2 a3
系统3 2 2005-10-3 a3
系统3 3.2 2005-10-4 a3
系统3 3.2 2005-10-5 a3
系统3 2 2005-10-6 a3
系统3 3 2005-10-7 a3

zzit0721 2005-10-12
  • 打赏
  • 举报
回复
128列,说错了,呵呵
分几张表进行存储的
zzit0721 2005-10-12
  • 打赏
  • 举报
回复
我现在列有500多列,我不可能就这么简单用合并插入来做吧
我想做个列的循环,让他自己循环列,然后进行插入
samfeng_2003 2005-10-12
  • 打赏
  • 举报
回复
哦,不好意思!看错了!:(
samfeng_2003 2005-10-12
  • 打赏
  • 举报
回复
[create] table a
(日期 varchar(20),a1 varchar(20),a2 varchar(20),a3 varchar(20))
insert a
select '2005-10-1','1','1.3','.14' [union] all
select '2005-10-2','3','2.2','2.1' [union] all
select '2005-10-3','2','2.2','2.1'

[create] table b
(col1 varchar(20),col2 varchar(20))

insert b
select '系统1','a1' [union] all
select '系统2','a2' [union] all
select '系统3','a3'

select col1 as 表名称,a.a1 as 值,a.日期 as 日期,col2 as 表id into # from a,b order by b.col1
select * from #
drop table #
drop table b
drop table a


表名称 值 日期 表id
-------------------- -------------------- -------------------- --------------------
系统1 1 2005-10-1 a1
系统1 3 2005-10-2 a1
系统1 2 2005-10-3 a1
系统2 2 2005-10-3 a2
系统2 3 2005-10-2 a2
系统2 1 2005-10-1 a2
系统3 1 2005-10-1 a3
系统3 3 2005-10-2 a3
系统3 2 2005-10-3 a3
子陌红尘 2005-10-12
  • 打赏
  • 举报
回复
--生成测试数据
create table #a(日期 datetime,A1 numeric(5,2),A2 numeric(5,2),A3 numeric(5,2))
insert into #a select '2005-10-1',1 ,1.3,0.14
insert into #a select '2005-10-2',3 ,2.2,2.1
insert into #a select '2005-10-3',2 ,2.2,2.1
insert into #a select '2005-10-4',3.2,3.1,3.1
insert into #a select '2005-10-5',3.2,3 ,2
insert into #a select '2005-10-6',2 ,2 ,2.1
insert into #a select '2005-10-7',3 ,4 ,5
create table #b(表名称 varchar(20),表ID varchar(20))
insert into #b select '系统1','A1'
insert into #b select '系统2','A2'
insert into #b select '系统3','A3'

--执行新表创建操作
select c.* into #c
from
(select b.表名称,值=a.A1,a.日期,b.表ID from #a a,#b b where b.表ID='A1'
union all
select b.表名称,值=a.A2,a.日期,b.表ID from #a a,#b b where b.表ID='A2'
union all
select b.表名称,值=a.A3,a.日期,b.表ID from #a a,#b b where b.表ID='A3') c
order by
c.表名称,c.日期

--查看结果
select * from #c

--输出结果
/*
表名称 值 日期 表ID
------- ------ ------------------------- ----
系统1 1.00 2005-10-01 00:00:00.000 A1
系统1 3.00 2005-10-02 00:00:00.000 A1
系统1 2.00 2005-10-03 00:00:00.000 A1
系统1 3.20 2005-10-04 00:00:00.000 A1
系统1 3.20 2005-10-05 00:00:00.000 A1
系统1 2.00 2005-10-06 00:00:00.000 A1
系统1 3.00 2005-10-07 00:00:00.000 A1
系统2 1.30 2005-10-01 00:00:00.000 A2
系统2 2.20 2005-10-02 00:00:00.000 A2
系统2 2.20 2005-10-03 00:00:00.000 A2
系统2 3.10 2005-10-04 00:00:00.000 A2
系统2 3.00 2005-10-05 00:00:00.000 A2
系统2 2.00 2005-10-06 00:00:00.000 A2
系统2 4.00 2005-10-07 00:00:00.000 A2
系统3 0.14 2005-10-01 00:00:00.000 A3
系统3 2.10 2005-10-02 00:00:00.000 A3
系统3 2.10 2005-10-03 00:00:00.000 A3
系统3 3.10 2005-10-04 00:00:00.000 A3
系统3 2.00 2005-10-05 00:00:00.000 A3
系统3 2.10 2005-10-06 00:00:00.000 A3
系统3 5.00 2005-10-07 00:00:00.000 A3
*/

--删除测试数据
drop table #a,#b,#c
churchatp1 2005-10-12
  • 打赏
  • 举报
回复

select b.表名称,a.* into #t from (
select 'A1' as 表id,日期,A1 from a
union all select 'A2' as 表id,日期,a2 from a
union all select 'A3' as 表id,日期,a3 from a
) as a,b
where a.表id=b.表id
子陌红尘 2005-10-12
  • 打赏
  • 举报
回复
select c.* into 新表
from
(select b.表名称,a.A1,a.日期,b.表ID from a,b where b.表ID='A1'
union all
select b.表名称,a.A2,a.日期,b.表ID from a,b where b.表ID='A2'
union all
select b.表名称,a.A3,a.日期,b.表ID from a,b where b.表ID='A3') c
order by
c.表名称,c.日期

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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