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