Sql 如何在子查询中放置内部窗口函数
我试图对窗口函数的输出求和。问题是,当我尝试执行以下操作时,它会引发一个错误Sql 如何在子查询中放置内部窗口函数,sql,Sql,我试图对窗口函数的输出求和。问题是,当我尝试执行以下操作时,它会引发一个错误 SELECT cm.cust_id, SUM(SUM(tr.FD_sales) OVER (partition by cm.cust_id order by tr.event_date range between interval 7 days preceding and current row)) AS T FROM campaign_engagement_history cm
SELECT
cm.cust_id,
SUM(SUM(tr.FD_sales) OVER (partition by cm.cust_id order by tr.event_date range between interval 7 days preceding and current row)) AS T
FROM
campaign_engagement_history cm
LEFT JOIN
transactions_filtered tr ON cm.cust_id == tr.cust_id
AND DATE(tr.EVENT_DATE) BETWEEN DATE(date_add(cm.campaign_send_date, -90)) AND DATE(cm.campaign_send_date)
GROUP BY
cm.cust_id
有人对如何解决这个问题有什么建议吗?我不知道在子查询中使用窗口函数会是什么样子
这就是错误:
It is not allowed to use a window function inside an aggregate function. Please use the inner window function in a sub-query.
首先使用窗口函数选择查询,然后使用外部函数将其包装,以便使用group by子句对窗口函数的值求和:
select cust_id,sum(T) from
(
SELECT
cm.cust_id,
SUM(tr.FD_sales) OVER (partition by cm.cust_id order by tr.event_date range between interval 7 days preceding and current row) AS T
FROM
campaign_engagement_history cm
LEFT JOIN
transactions_filtered tr ON cm.cust_id == tr.cust_id
AND DATE(tr.EVENT_DATE) BETWEEN DATE(date_add(cm.campaign_send_date, -90)) AND DATE(cm.campaign_send_date)
) t
GROUP BY
cust_id
请提供样本数据和预期结果。不清楚你想做什么。@Sam Comber你试过这个吗?如果你面临更多的困难,请告诉我。