SQL capped, continuous sum

强颜欢笑 提交于 2019-12-03 20:53:38

This solution uses a recursive-cte which isn't supported in HANA (as per the OP). Posting a solution which works with databases that support it.

WITH ROWNUMS AS
 (SELECT T.*,
         ROW_NUMBER() OVER(ORDER BY ROW) AS RNUM
  FROM T) 
,RCTE AS
 (SELECT ROW,
         RNUM,
         MEASURE,
         MEASURE AS CAPPED_SUM
  FROM ROWNUMS
  WHERE RNUM=1
   UNION ALL
   SELECT RN.ROW,
          RN.RNUM,
          RN.MEASURE,
          CASE
              WHEN R.CAPPED_SUM+RN.MEASURE>=2 THEN 2
              ELSE R.CAPPED_SUM+RN.MEASURE
          END
   FROM ROWNUMS RN
   JOIN RCTE R ON R.RNUM=RN.RNUM-1 )
SELECT ROW,
       MEASURE,
       CAPPED_SUM
FROM RCTE

Sample Demo

This script first creates a column of a running sum. Then it uses that column to create a column of "overage", how much the running sum exceeds the capped value, cumulatively. Then it subtracts the overage in order to give a value less than 2 if appropriate.

DECLARE @capped_value INT = 2
;WITH CTE AS
(SELECT rowID,measure,
    running_total = SUM(measure) OVER 
    (ORDER BY rowID ROWS UNBOUNDED PRECEDING)
FROM dbo.test_capped_sum)
,
CTE2 AS
(SELECT *,
    overage_total = MAX(CTE.running_total) 
    OVER (ORDER BY rowID ROWS UNBOUNDED PRECEDING) - @capped_value
FROM CTE)

SELECT rowid,measure,
    CASE WHEN CTE2.overage_total > 0
    THEN CTE2.running_total- CTE2.overage_total 
    ELSE CTE2.running_total END
    AS capped_sum FROM CTE2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!