问一句SQL: 表A,B结构相同 (CODE,NUM)
现表A数据如下: 表B数据如下:
CODE NUM CODE NUM
1 100 1 10
2 100 2 10
3 100 3 10
4 100 2 10
5 100 1 10
要求得到表C表C所得数据如下:
CODE NUM
1 80 <---------100-10-10
2 80 <---------100-10-10
3 90 <---------100-10
4 100 <---------100
5 100 <---------100
只要一句SQL,谢谢
现表A数据如下: 表B数据如下:
CODE NUM CODE NUM
1 100 1 10
2 100 2 10
3 100 3 10
4 100 2 10
5 100 1 10
要求得到表C表C所得数据如下:
CODE NUM
1 80 <---------100-10-10
2 80 <---------100-10-10
3 90 <---------100-10
4 100 <---------100
5 100 <---------100
只要一句SQL,谢谢
解决方案 »
- 怎样在加个条件,如年龄最大那个
- 表中的代码如何替换用另一表中对应的值来替换?
- 数据库附加出错,急!1
- 请教个有关修改的问题!急呀!
- SQL语句能不能写文件操作的?
- windows 2003 server下安装sql server2000总是提示说不支持,怎么办?
- 问几个关于sqlserver分区的问题
- 关于sql server2008 访问oracle11g中表同义词的的问题
- 向大牛们求教一个对小弟来说超难的sql
- 现有sqlserver7.0的数据文件(.mdf和.ldf),请问有恩有办法使它在sqlserver2000下运行
- 请问在sql2000中如何查看存储过程的修改日期?
- 求一SQL语句
(code int,
num int
)
insert into @a
select 1,100
union all
select 2,100
union all
select 3,100
union all
select 4,100
union all
select 5,100
declare @b table
(
code int,
num int
)
insert into @b
select 1,10
union all
select 2,10
union all
select 3,10
union all
select 2,10
union all
select 1,10select a.code,
(a.num-isnull((select sum(num) from @b where a.code=code),0)) as Num
from @a a
from A t
declare @a table (code int , num int )
declare @b table (code int , num int )
insert into @a select 1,100
union all select 2,100
union all select 3,100
union all select 4,100
union all select 5,100
insert into @b select 1,10
union all select 2,10
union all select 3,10
union all select 2,10
union all select 1,10
--语句
select code , num - isnull((select sum(num) from @b where code =a.code) ,0) [num]
from @a a
from a left join b on a.code=b.code
group by a.code,a.num