WITH [Rollup]
AS
(
SELECT id, parent, 0 AS depth FROM RollupAccount 
WHERE id = 1000
UNION ALL
SELECT R.id, R.parent, depth + 1 AS depth FROM
RollupAccount AS R
JOIN
[Rollup] AS RP
ON R.parent = RP.ID
WHERE depth < 1
)
SELECT SUM(balance) AS Balance
FROM DetailAccount
JOIN
[Rollup]
ON DetailAccount.parent = Rollup.id
a
b
c
