我是pentaho的新手。基本上我对层次结构数据有一个要求,我选择一个层次结构的子集,我需要在较低级别的节点上进行计数/求和,然后逐级进行计数+先前的计数总和等。这就是我所知道的使用表变量执行 sql(这里的要点是我需要在内存中执行此操作,而不是在数据表中执行任何更新。)
DECLARE @ConsolidateMaster TABLE
(
-- my fields
)
INSERT @ConsolidateMaster
--select some sub set data and inset into table variable
UPDATE A
SET A.[DIR_REPORT_CNT] = 0,
A.[DIR_REPORT_COST] = 0,
A.[TOTAL_REPORT_CNT] = 0,
A.[TOTAL_REPORT_COST] = 0
FROM @ConsolidateMaster A
LEFT JOIN @ConsolidateMaster B
ON A.EMP_ID = B.SUPERVISOR_ID
WHERE B.EMP_ID IS NULL AND A.PROJ_CD = @PROJ_CD
--GO UP LEVEL BY LEVEL AND START COUNTING THE TOTAL + COST
DECLARE @MAX_LEVEL INT = (SELECT MAX(SUPERVISOR_LAYER) FROM @ConsolidateMaster WHERE PROJ_CD = @PROJ_CD)
WHILE (SELECT COUNT(1) FROM @ConsolidateMaster WHERE [DIR_REPORT_CNT] IS NULL) > 0
BEGIN
UPDATE
A
SET
A.DIR_REPORT_CNT = B.DIR_REPORT_CNT,
A.DIR_REPORT_COST = B.DIR_REPORT_COST,
A.TOTAL_REPORT_CNT = B.TOTAL_REPORT_CNT,
A.TOTAL_REPORT_COST = B.TOTAL_REPORT_COST
FROM @ConsolidateMaster A
INNER JOIN
(
SELECT
C.SUPERVISOR_ID,
COUNT(1) DIR_REPORT_CNT,
SUM(ISNULL(ANNUAL_COST,0)) DIR_REPORT_COST,
SUM(C.[TOTAL_REPORT_CNT]) + COUNT(1) TOTAL_REPORT_CNT,
SUM(C.TOTAL_REPORT_COST) + SUM(ISNULL(ANNUAL_COST,0)) TOTAL_REPORT_COST
FROM @ConsolidateMaster C
WHERE
C.SUPERVISOR_LAYER = @MAX_LEVEL
AND C.PROJ_CD = @PROJ_CD
GROUP BY
C.SUPERVISOR_ID
) B
ON A.EMP_ID = B.SUPERVISOR_ID
WHERE A.PROJ_CD = @PROJ_CD
AND A.SUPERVISOR_LAYER < @MAX_LEVEL
SET @MAX_LEVEL = @MAX_LEVEL - 1
END
在对 sql 中的 temp 变量进行更新后,我只需从 @ConsolidateMaster 中选择 * 。我如何在 pentaho 中实现类似的功能。