我正在运行以下代码来计算累积值
SELECT
iid
,item_id
,pg_purch_ind
,SUM_score1
,SUM(pg_purch_ind) OVER(PARTITION BY item_id ORDER BY SUM_score1 DESC, iid % 100, iid ) AS cum_pg_purch
into table2
FROM table1
但它有以下错误“'order'附近的语法不正确。”有谁知道这是什么问题?
答案 0 :(得分:1)
以下是较小版本的解决方法
;WITH cte
AS (SELECT iid,
item_id,
pg_purch_ind,
SUM_score1,
Row_number()OVER(partition BY item_id ORDER BY SUM_score1 DESC, iid % 100, iid ) AS rn
FROM Yourtable)
SELECT iid,
item_id,
pg_purch_ind,
SUM_score1,
cum_pg_purch
FROM cte a
CROSS apply (SELECT Sum (pg_purch_ind)
FROM cte b
WHERE a.item_id = b.item_id
AND b.rn <= a.rn) cs (cum_pg_purch)
由于模运算符
,在生成Row_Number
时很难处理交叉应用Where
子句