日期:2014-05-17 浏览次数:20502 次
if object_id('[a]') is not null drop table [a]
go
create table [a]([区分] int,[金额] int)
insert [a]
select 1,100 union all
select 1,200 union all
select 1,300 union all
select 3,100
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([金额] int)
insert [b]
select 50
--------------开始查询--------------------------
SELECT * FROM a
UPDATE a
SET a.金额=a.金额+(SELECT [金额] FROM b)
FROM a
WHERE EXISTS(SELECT 1 FROM (SELECT 区分,MAX(金额)金额 FROM a WHERE 区分 =(SELECT MIN(区分)区分 FROM a) GROUP BY 区分)b WHERE a.区分=b.区分 AND a.金额=b.金额)
SELECT * FROM a
/*
区分 金额
----------- -----------
1 100
1 200
1 300
3 100
(4 row(s) affected)
(1 row(s) affected)
区分 金额
----------- -----------
1 100
1 200
1 350
3 100
*/