mysql - 错误 : function avg(boolean) does not exist (MySql > PostgreSQL conversion)

标签 mysql sql postgresql

我有以下在 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/

相关文章:

mysql - 使用 WHILE 的 MySQL 函数中出现语法错误

sql - 在不同数据库服务器上的表之间传输行

java - IntelliJ IDEA - SQL Inside Java 代码的语法高亮

mysql - 如何对 SQL 中允许特定组计数为零的字段进行计数?

mysql - 如何获取 MySQL 数据库表的大小?

python - 我正在尝试使用 pip 安装 MySQL 库,但收到一个奇怪的错误

c# - 无法打开 mysql 连接

c# - 将 SQL 2008 DB R2 恢复到 SQL 2008(不是 r2)

postgresql - 在postgresql中划分大数

postgresql - postgres 流复制中主要落后于恢复