table1
UserId JinE
1 500.00
1 650.00
2 350.00
3 460.00
4 340.00
4 220.00
5 150.00table2
UserId BuTie
2 150.00
4 120.00
6 100.00
最终想要得到UserId Qian
1 1150.00
2 500.00
3 460.00
4 680.00
5 150.00
6 100.00
UserId JinE
1 500.00
1 650.00
2 350.00
3 460.00
4 340.00
4 220.00
5 150.00table2
UserId BuTie
2 150.00
4 120.00
6 100.00
最终想要得到UserId Qian
1 1150.00
2 500.00
3 460.00
4 680.00
5 150.00
6 100.00
解决方案 »
- 作业题,有个小错误
- sql数据导入导出的问题
- 菜鸟求教:DataCombo的自动完成功能-代码
- SQL生成.xls文件请教!
- 请问这种搜索语句怎么写,很苦恼。
- sql 2008 内存总是在1.6G左右
- 帮忙看看吧,大侠们!
- 怎样在触发器中修改inerted的数据?
- 100分求助:不够可以再加,一个表 其中没有自动编号字段,也没有时间字段 只有10个varchar字段. 请问我有没有办法判断今日的更新记录 ?
- 急!!!!!!!!!!!!急!!!!!!!!!!!!!!!一个用rose做sql server2000的群集问题...请给位大虾帮帮忙...谢谢........
- 两个表纵向相连接,还要创建一个ID,这个视图应该怎么写啊
- 明明告诉我执行成功,为什么使用的数据库没有变化
table2中的UserId是不会重复的。
sum(JintE) as Qian
from (select *
from table1
union all
select *
from table2) t
group by UserId
(select UserId, JinE as Qian
from table1
union all
select UserId, BuTie as Qian from table2 ) tmp
group by UserID
create table tb1
(userid int,
jine float(10)
)
create table tb2
(userid int,
butie float(10)
)insert tb1 values(1,500.00)
insert tb1 values(1,650.00)
insert tb1 values(2,350.00)
insert tb1 values(3,460.00)
insert tb1 values(4,340.00)
insert tb1 values(4,220.00)
insert tb1 values(5,150.00)insert tb1 values(2,150.00)
insert tb1 values(4,120.00)
insert tb1 values(6,100.00)
select userid,sum(jine) qian from
(select * from tb1
union all
select * from tb2) b
group by userid
--drop table tb1
--drop table tb2
userid qian
------------
1 1150
2 500
3 460
4 680
5 150
6 100
insert @t1 select 1,500.00
union all select 1,650.00
union all select 2,350.00
union all select 3,460.00
union all select 4,340.00
union all select 4,220.00
union all select 5,150.00declare @t2 table(Userid int,BuTie numeric(12,2))
insert @t2 select 2,150.00
union all select 4,120.00
union all select 6,100.00select userid,Qian=sum(JinE)
from(
select * from @t1 union all select * from @t2
)t
group by userid/*
userid Qian
----------- ----------------------------------------
1 1150.00
2 500.00
3 460.00
4 680.00
5 150.00
6 100.00(所影响的行数为 6 行)
*/