我有以下在 MariaDB 中运行良好的查询:
SELECT floor(datediff(r.created_at, curdate()) / 7) AS weeks_ago,
date(min(r.created_at)) AS "Date Start",
date(max(r.created_at)) AS "Date End",
count(*) as "Reviews in Cohort",
AVG(has_note) as "Reviews w 1 or more Notes Ratio"
FROM (SELECT r.id, r.created_at,
( MAX(rn.note) IS NOT NULL ) as has_note
FROM reviews f JOIN
reviewss_notes rn
ON r.id = rn.review_id
WHERE r.completed_at IS NOT NULL
AND r.created_at > '2019-01-01'
AND r.type = "long_form"
GROUP BY r.id
) f
GROUP BY weeks_ago
ORDER BY weeks_ago DESC;
我正在努力更新查询以在 PostgreSQL 中工作。这是我最近的尝试:
SELECT TRUNC(DATE_PART('day', CURRENT_DATE - r.created_at )/7) AS weeks_ago,
date(min(r.created_at)) AS "Date Start",
date(max(r.created_at)) AS "Date End",
count(*) as "Reviews in Cohort",
AVG(has_note) as "Reviews w 1 or more Notes Ratio"
FROM (SELECT r.id, r.created_at,
( MAX(rn.note) IS NOT NULL ) as has_note
FROM reviews f JOIN
reviewss_notes rn
ON r.id = rn.review_id
WHERE r.completed_at IS NOT NULL
AND r.created_at > '2019-01-01'
AND r.type = "long_form"
GROUP BY r.id
) f
GROUP BY weeks_ago
ORDER BY weeks_ago DESC;
我从 PostgreSQL 得到的错误是:
ERROR: function avg(boolean) does not exist
LINE 5: AVG(has_note) as "Reviews w 1 or more Notes Ratio"
^
HINT: No function matches the given name and argument types.
有什么想法吗? PostgreSQL 不允许这种模式吗?
最佳答案
只需转换为数字:
AVG(has_note::int) as "Reviews w 1 or more Notes Ratio"
关于mysql - 错误 : function avg(boolean) does not exist (MySql > PostgreSQL conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57503537/