我试图通过在Amazon Redshift上使用以下查询找出retention rate
:
WITH t AS (
SELECT ga.ownerid,
DATE_PART('month',ga.creationtime) AS month,
COUNT(*) AS item_transactions,
LAG(DATE_PART('month',ga.creationtime)) OVER (PARTITION BY ownerid ORDER BY DATE_PART('month',ga.creationtime)) = DATE_PART('month',ga.creationtime) -interval '1 month' OR NULL AS repeat_transaction
FROM flx2.groupactivities ga
JOIN auth.members m ON ga.ownerid = m.id
WHERE ga.activitytype = 'assign'
AND ga.groupid NOT IN (SELECT groupid
FROM (SELECT groupid,
COUNT(DISTINCT memberid)
FROM flx2.grouphasmembers
GROUP BY groupid
HAVING COUNT(DISTINCT memberid) = 1))
AND ga.ownerid IN (SELECT memberid FROM auth.memberhasroles WHERE roleid = 5)
AND ga.ownerid NOT IN (SELECT memberid FROM auth.memberhasroles WHERE roleid = 25)
GROUP BY ga.ownerid,
DATE_TRUNC('month',ga.creationtime)
ORDER BY ga.ownerid,
DATE_TRUNC('month',ga.creationtime)
)
SELECT month
,sum(item_transactions) AS num_trans
,count(*) AS num_buyers
,count(repeat_transaction) AS repeat_buyers
,round(
CASE WHEN sum(item_transactions) > 0
THEN count(repeat_transaction) / sum(item_transactions) * 100
ELSE 0
END, 2) AS buyer_retention
FROM t
GROUP BY 1
ORDER BY 1;
但是它给了我以下错误:
An error occurred when executing the SQL command:
WITH t AS (
SELECT ga.ownerid,
DATE_TRUNC('month',ga.creationtime) AS month,
COUNT(*) AS item_transactions,
LAG(DATE_TRUNC('month...
[Amazon](500310) Invalid operation: ORDER/GROUP BY expression not found in targetlist;
Execution time: 0.29s
1 statement failed.
我认为这里的LAG
功能有问题,但我不太确定。我从帖子here收到了此查询,并根据我的要求对其进行了修改。
请有人帮我解决这里出了什么问题吗?
我提前感谢你的帮助。
答案 0 :(得分:0)
快速查看,但lag
本身不是汇总函数,因此repeat_transaction
需要包含group by
。