是否可以映射此内容,而不是遍历它? 在某些情况下,不应将值放在地图中,因此我想忽略这些情况:
WITH ncte AS (
SELECT CustomerName
, SUM(TotalSales) AS CustomerTotal
, SUM(SUM(TotalSales)) OVER () AS GrandTotal
, ROW_NUMBER() OVER (ORDER BY SUM(TotalSales) DESC) AS RN
FROM customers
GROUP BY CustomerName
), rcte AS (
SELECT base.CustomerName
, base.CustomerTotal
, base.GrandTotal
, base.RN
, base.CustomerTotal AS RunningTotal
, 1 AS CL
FROM ncte AS base
WHERE base.RN = 1
UNION ALL
SELECT curr.CustomerName
, curr.CustomerTotal
, curr.GrandTotal
, curr.RN
, IIF(prev.RunningTotal < CHOOSE(prev.CL, .4, .3, .2, .1) * prev.GrandTotal, prev.RunningTotal, 0) + curr.CustomerTotal
, IIF(prev.RunningTotal < CHOOSE(prev.CL, .4, .3, .2, .1) * prev.GrandTotal, prev.CL, prev.CL + 1)
FROM ncte AS curr
JOIN rcte AS prev ON curr.RN = prev.RN + 1
)
SELECT *
FROM rcte