611
社区成员
发帖
与我相关
我的任务
分享
declare @t table(bm varchar(10),xm varchar(10),je decimal(5,0))
insert into @t
select '部门1','项目1',100 union all
select '部门1','项目2',200 union all
select '部门1','项目3',100 union all
select '部门1','项目4',300 union all
select '部门2','项目4',500
--select * from @t
select b.bm,b.xm,b.je,
(select sum(isnull(a.je,0)) from @t a where a.bm = b.bm ) as '项目金额和'
from @t b
/*
(所影响的行数为 5 行)
bm xm je 项目金额和
---------- ---------- ------- ------------
部门1 项目1 100 700
部门1 项目2 200 700
部门1 项目3 100 700
部门1 项目4 300 700
部门2 项目4 500 500
(所影响的行数为 5 行)
*/