我有三张表,表结构是一致的,有序号(标识列),编号,数量,金额,我想把三个表中对应编号的"数量"和"金额"列合并,生成新的同样的表,请问怎么做?
如:表1:
序号 编号 数量 金额
1 a01 61 10
2 a02 50 20
3 a03 20 30表2:
序号 编号 数量 金额
1 a01 20 23
2 a02 10 51
3 a04 15 30表3类似
生成表A:序号 编号 数量 金额
1 a01 81 33
2 a02 60 71
3 a03 20 30
4 a04 15 30
如:表1:
序号 编号 数量 金额
1 a01 61 10
2 a02 50 20
3 a03 20 30表2:
序号 编号 数量 金额
1 a01 20 23
2 a02 10 51
3 a04 15 30表3类似
生成表A:序号 编号 数量 金额
1 a01 81 33
2 a02 60 71
3 a03 20 30
4 a04 15 30
解决方案 »
- 请教2个sql语句怎么写?
- 哪位大侠知道怎么用存储过程还是什么访问URL地址呢?xp_cmdshell能用么?
- 问:为什么全文检索contains(字段,'xyz')比like'%xyz%'少记录呢?求解!
- sql 语句题目,,,,求帮助
- connect fail的问题,紧急求助啊,有没好心人帮忙看看啊
- 请教一个字符串问题
- 有些复杂的函数问题。。。
- 一个带有游标的过程,出错提示是 “过程没有参数,但却为该过程提供了参数”,不知道为什么。
- 能用一条SQL语句把SQL SERVER的表倒进DBF文件吗?
- Sql Server高手请进,关于复制问题要其叫请教
- 知道一个表的字段名称(字符串形式), 怎么取此列的内容?
- 建立触发器之后,如果表结构改变了,会产生什么情况,什么影响。
from (select * from 表1 as a, 表2 as b
where a.编号 = b.编号) as c
group by 编号
上面好像不对
=========================select 序号, 编号 ,sum( 数量 )as 数量,sum( 金额) as 金额
from (select * from 表1 union all select * from 表2 ) as c
group by 编号
from
(
select 编号, 数量, 金额 from T1
union all
select 编号, 数量, 金额 from T2
union all
select 编号, 数量, 金额 from T3
)tmp group by tmp.编号
into newtablename
from
(select 编号,数量,金额 from 表1
union
select 编号,数量,金额 from 表2
union
select 编号,数量,金额 from 表3) as a
group by 编号
create Table T1(序号 int, 编号 char(3), 数量 int, 金额 int)
insert T1 select 1, 'a01', 61, 10
union all select 2, 'a02', 50, 20
union all select 3, 'a03', 20, 30create Table T2(序号 int, 编号 char(3), 数量 int, 金额 int)
insert T2 select 1, 'a01', 20, 23
union all select 2, 'a02', 10, 51
union all select 3, 'a04', 15, 30create Table T3(序号 int, 编号 char(3), 数量 int, 金额 int)select ID=identity(int, 1, 1), tmp.编号, sum(tmp.数量) as 数量, sum(tmp.金额) as 金额 into A
from
(
select 编号, 数量, 金额 from T1
union all
select 编号, 数量, 金额 from T2
union all
select 编号, 数量, 金额 from T3
)tmp group by tmp.编号select * from A
--result
ID 编号 数量 金额
----------- ---- ----------- -----------
1 a01 81 33
2 a02 60 71
3 a03 20 30
4 a04 15 30(4 row(s) affected)