我这里有两张数据表,结构完全一样,
table1和table2表。字段定义为:
Num int 4 (为主键)
sl varchar2
(我用的是sqlserver200)
第一个表的内容如下:
1001 100
1002 220
1003 213
第二个表的内容如下:
1001 110
1002 120
1003 310
如何实现建立第三个table3,而它的结构和table1和table2完全一样,内容如下:
1001 210
1002 340
1003 523
请各位高手帮忙,问题解决马上给分!!!
table1和table2表。字段定义为:
Num int 4 (为主键)
sl varchar2
(我用的是sqlserver200)
第一个表的内容如下:
1001 100
1002 220
1003 213
第二个表的内容如下:
1001 110
1002 120
1003 310
如何实现建立第三个table3,而它的结构和table1和table2完全一样,内容如下:
1001 210
1002 340
1003 523
请各位高手帮忙,问题解决马上给分!!!
(
Num int 4 not null primay key,
sl varchar(20) null
)insert intto table3
select num as num,sum(sl) as sl from
(
select num,sl from table1
union
select num,sl from table2
) aaa
group by num
select Num,Convert(bigInt,S1)+(select Convert(bigInt,S1) from Table2 where Table2.Num=Table1.num) from Table1//BigInt 或 Int 根据你的Varchar字段中数据的大小而定,若超出Int的则使用BigInt
(
Num int 4 not null primay key,
sl varchar(20) null
)insert intto table3
select num,cast(sl as varchar) from
(
select num as num,sum(sl) as sl from
(
select num,cast(sl as int) from table1
union
select num,cast(sl as int) from table2
) aaa
group by num
)bbb
select a.id,a.value+b.value into c where a.id=b.id正解
我是想实现部分数据相加还有部分数据相减,如table1和table2的1001和1003的sl数据相加,
1002的sl数据相减。然后汇总到table3表.我这个问题的意思是想直接用delphi代码控制,我之前用过一个TBatchMove控件,只能实现数据的追加和更新,所以不合适,不知是否有类似的控件。不好意思,昨天一天停电,没能来得及看帖,报歉!!!!
while not qry.eof do begin
...
qry.next
end;
语句逐条判断了如果判断条件少的话可以select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
union {--all}
select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
into c
其中all表示不去掉重复记录
当然c必须要先存在
union {--all}
select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
into c
select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
union {--all}
select a.id,a.value+b.value where a.id=b.id and a.id=xxxx
其中all表示不去掉重复记录
当然c必须要先存在
搞定收工..