我有两个表:
- 客户:id,joining_date
- 订单:id、customer_id、order_date
我需要选择加入日期后6个月内下订单的客户数量。
到目前为止我做了什么:
SELECT COUNT(*)
FROM customers
JOIN orders
ON customers.id = orders.customer_id
AND (
orders.order_date BETWEEN customers.joining_date
AND DateAdd(month,1,customers.joining_date)
)
但是我得到了
Error: column 'month' does not exist
最佳答案
您正在使用 SQL Server 语法
在 PostgreSQL 中,您添加一个时间间隔 date + interval '1 month'
因此,在您的情况下,您应该将 DateAdd 替换为:
customers.joining_date + interval '6 month'
关于6 个月内下订单的客户数量的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053277/