POSTGRESQL-“;错误:运算符不存在:间隔<;=日期“;

POSTGRESQL-“;错误:运算符不存在:间隔<;=日期“;,sql,database,postgresql,types,Sql,Database,Postgresql,Types,尝试创建显示活动/非活动帐户的案例字段。活跃客户已在过去90天内下单 我遇到了数据类型问题,无法找到解决方案。下面是我试图运行的代码: SELECT DISTINCT ON (accountid) accountid, customer, round(AVG(qty),2) AS average_order_qty, ROUND(AVG(total),2) AS avg_order_rev, (CURRENT_DATE - MAX(date)) as days_since_order,

尝试创建显示活动/非活动帐户的案例字段。活跃客户已在过去90天内下单

我遇到了数据类型问题,无法找到解决方案。下面是我试图运行的代码:

SELECT DISTINCT ON (accountid)
    accountid, customer, round(AVG(qty),2) AS average_order_qty, ROUND(AVG(total),2) AS avg_order_rev, (CURRENT_DATE - MAX(date)) as days_since_order, (CURRENT_DATE - MAX(date)) as active,
        CASE 
            WHEN (CURRENT_DATE - MAX(date)) <= CURRENT_DATE - 90 THEN 'Yes'
            ELSE 'No'
        END
FROM m2m_kit_total
GROUP BY accountid, customer
ORDER BY accountid, MAX(date) DESC NULLS LAST;
在(accountid)上选择DISTINCT
accountid,customer,round(平均(数量),2)作为平均订单数量,round(平均(总数),2)作为平均订单版本,(当前日期-最大(日期))作为自订单起的天数,(当前日期-最大(日期))作为活动,
案例

当(当前_日期-最大(日期))时,请尝试运行以下命令:

SELECT DISTINCT ON (accountid)
    accountid, customer, round(AVG(qty),2) AS average_order_qty, ROUND(AVG(total),2) AS avg_order_rev, (CURRENT_DATE - MAX(date)) as days_since_order, (CURRENT_DATE - MAX(date)) as active,
        CASE 
            WHEN ((CURRENT_DATE - MAX(date)) = (CURRENT_DATE - 90)) THEN 'Yes'
            ELSE 'No'
        END
FROM m2m_kit_total
GROUP BY accountid, customer
ORDER BY accountid, MAX(date) DESC NULLS LAST;

我相信这就是你想要的逻辑:

SELECT accountid, customer, round(AVG(qty), 2) AS average_order_qty, 
       ROUND(AVG(total), 2) AS avg_order_rev,
       (CURRENT_DATE - MAX(date)) as days_since_order,
       (CASE WHEN MAX(date) >= CURRENT_DATE - INTERVAL '90 DAY' THEN 'Yes'
             ELSE 'No'
        END) as is_active
FROM m2m_kit_total
GROUP BY accountid, customer
ORDER BY accountid, MAX(date) DESC NULLS LAST;
这是两个日期的简单比较。它不应该有任何问题


我还猜测每个帐户只有一个客户,因此每个
accountid
返回一行,而不依赖
distinct on

错误:运算符不存在:间隔=日期行4:WHEN((当前日期-最大(日期))=(当前日期…^提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型强制转换。SQL状态:42883字符:280这90是什么?您正在减去它?它是日期还是天数/它是天数。Gordon在上面的回答中解决了我的问题。感谢您的帮助!您知道您的条件<代码>(当前日期-最大(日期))=90
?是的!谢谢,戈登!工作得很好。
SELECT accountid, customer, round(AVG(qty), 2) AS average_order_qty, 
       ROUND(AVG(total), 2) AS avg_order_rev,
       (CURRENT_DATE - MAX(date)) as days_since_order,
       (CASE WHEN MAX(date) >= CURRENT_DATE - INTERVAL '90 DAY' THEN 'Yes'
             ELSE 'No'
        END) as is_active
FROM m2m_kit_total
GROUP BY accountid, customer
ORDER BY accountid, MAX(date) DESC NULLS LAST;