IF NOT OBJECT_ID('[tbTest]') IS NULL DROP TABLE [tbTest] GO CREATE TABLE tbTest ( id nvarchar(5), parentID nvarchar(5), score decimal(18,2) ) insert tbTest select '1','',10 union all select '2','1',30 union all select '3','2',30 union all select '4','1',50 union all select '5','',10 union all select '6','5',30 GO /* select * from tbTest id parentID score ----- -------- --------------------------------------- 1 10.00 2 1 30.00 3 2 30.00 4 1 50.00 5 10.00 6 5 30.00 (6 行受影响) */ /* --要得到如下结果 id parentid score ----- -------- --------------------------------------- 1 130.00 2 1 60.00 3 2 30.00 4 1 50.00 5 40.00 6 5 30.00 (6 行受影响) */ ;with t as ( select *,total = score from tbTest as a where not exists(select * from tbTest where a.id = parentid) union all select a.*,cast(b.total + a.score as decimal(18,2)) from tbTest as a join t as b on a.id = b.parentid ) select id,parentid,sum(total) as score from t group by id,parentid order by id /* id parentid score ----- -------- ------------- 1 130.00 2 1 60.00 3 2 30.00 4 1 50.00 5 40.00 6 5 30.00 (6 行受影响) */