我正在使用 MS Project Server 编写报告,但遇到了问题。我需要总结子父层次结构中的一些字段。总和是叶子中值的总和。我写了一个查询,但它只适用于小数据集。
Task - 10
Task - 2
Task - 6
Task - 6
Task - 2
WITH children AS (
SELECT
tt.ID,
CASE '1' WHEN ISNULL(ttt.Id, '1') THEN tt.Value ELSE 0 END as Value,
CASE '1' WHEN ISNULL(ttt.Id, '1') THEN tt.Value2 ELSE 0 END as Value2,
tt.parentid
FROM [MonitoringExampleDatabase].[dbo].[Table_1] tt
LEFT JOIN [MonitoringExampleDatabase].[dbo].[Table_1] ttt
ON tt.ID = ttt.ParentId
UNION ALL
SELECT t.id, children.value, children.Value2, t.parentid
FROM children
JOIN [MonitoringExampleDatabase].[dbo].[Table_1] t ON children.parentid = t.id
)
SELECT id, sum(value) as Value, SUM(value2) as Value2
FROM children
GROUP BY id
任何如何优化该查询的想法,现在的问题是递归限制。