DATE_PART 和 PostgreSQL

DATE_PART and Postgresql

我在此查询中的函数 DATE_PART 中减去两个日期时遇到问题。

SELECT
    TO_CHAR(date_trunc('month',sql_activity_days.created_month),'YYYY-MM') AS "sql_activity_days.created_month",
    coalesce(SUM(
        CASE
            WHEN(date_part('day', (sql_activity_days.sale_date + 1) - sql_activity_days.start_date) < 122) 
             THEN sql_activity_days.cad_net_invoiced
            ELSE NULL
        END
    ),0) AS "sql_activity_days.activity_over_122_day_after_signup"
FROM
    camel.f_subscription_touch  AS subscription_touch
    LEFT JOIN sql_activity_days ON subscription_touch.id  = sql_activity_days.customer_id 
    group by date_trunc('month',sql_activity_days.created_month) 
    order by 1 desc limit
500

PostgreSQL 数据库在 运行 此查询时遇到错误。

ERROR: function date_part(unknown, integer) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 1340

function date_part 的第二个参数可以是 timestampinterval

您的表达式 (sql_activity_days.sale_date + 1) - sql_activity_days.start_date 减去两个 dates,其结果数据类型为整数,因此出现错误。

解决方法是删除 date_part 并直接使用表达式。两个日期之差总是给出以天为单位的值。