declare @t table (f1 varchar(20),f2 int,f3 varchar(20)) insert @t select '化验费',null,'化验费' union all select '1--d',87,'化验费' union all select '2--c',90,'化验费' union all select '检查费',null,'检查费' union all select '1--a',78,'检查费' union all select '2--b',100,'检查费' select f1,case when f1=f3 then (select sum(f2) from @t a where a.f3=b.f3 and a.f1<>a.f3) else f2 end,f3 from @t bf1 f3 -------------------- ----------- -------------------- 化验费 177 化验费 1--d 87 化验费 2--c 90 化验费 检查费 178 检查费 1--a 78 检查费 2--b 100 检查费(所影响的行数为 6 行)
(f1 varchar(20),f2 int,f3 varchar(20))
insert @t
select '化验费',null,'化验费' union all
select '1--d',87,'化验费' union all
select '2--c',90,'化验费' union all
select '检查费',null,'检查费' union all
select '1--a',78,'检查费' union all
select '2--b',100,'检查费'
select f1,case when f1=f3 then (select sum(f2) from @t a where a.f3=b.f3 and a.f1<>a.f3)
else f2 end,f3 from @t bf1 f3
-------------------- ----------- --------------------
化验费 177 化验费
1--d 87 化验费
2--c 90 化验费
检查费 178 检查费
1--a 78 检查费
2--b 100 检查费(所影响的行数为 6 行)