我正在尝试进行群组分析 - 尝试计算保留率。这是我的代码:
SELECT
renter_id,
min(DATE_PART('year', created_at)) AS first_rental_year,
DATE_PART('year', created_ay) AS y1,
round(100 * count(distinct b2.renter_id) /
count(distinct b1.renter_id)) AS retention
FROM bookings AS b1
LEFT JOIN bookings AS b2 ON
b1.renter_id = b2.renter_id
AND DATE_PART('year', b1.created_at) = DATE_PART(datetime('year', b2.created_at, '-1 year'))
GROUP BY 1
ORDER BY 2;
它根本不起作用...... The error message I get says: Hint: No function matches the given name and argument types. You might need to add explicit type casts.
一些建议会很棒。
答案 0 :(得分:0)
直接问题在于PostgreSQL中不存在的datetime()
函数。然后是别名和聚合的一些问题。这可以让你更接近你想要的地方:
SELECT
b1.renter_id,
min(DATE_PART('year', b1.created_at)) AS first_rental_year,
DATE_PART('year', created_ay) AS y1, -- Needs an aggregate
round(100. * count(distinct b2.renter_id) / -- Use float, see 100.
count(distinct b1.renter_id)) AS retention
FROM bookings AS b1
LEFT JOIN bookings AS b2
ON b1.renter_id = b2.renter_id
AND extract(year, b1.created_at) + 1 = extract(year, b2.created_at)
GROUP BY 1
ORDER BY 2;
但是,您的群组功能的逻辑有缺陷。您应该将问题编辑为: