日期:2014-05-16 浏览次数:20433 次
;WITH f AS
(
SELECT *,1 AS LEVEL FROM tb AS a WHERE NOT EXISTS(SELECT 1 FROM tb WHERE id=a.parentId)
UNION ALL
SELECT a.*,level+1 FROM tb AS a INNER JOIN f AS b ON a.parentId=b.id
)
SELECT *,ISNULL((SELECT SUM(Bail) AS Bail FROM f WHERE level>=a.level),bail) AS SUMBail FROM f AS a