我有一张订单表,其中包含客户编号和完成日期。我正在尝试运行查询以查看哪些客户未订购6个月。我尝试了以下方法:
SELECT DISTINCT
orders.Customer,
Max(orders.DateFinished)
FROM
orders
WHERE
DATEDIFF(CURDATE(), Max(orders.DateFinished)) > 180;
GROUP BY
orders.Customer
但是我得到了
[Err] 1111 - 无效使用群组功能
答案 0 :(得分:0)
SELECT Customer,
Max(DateFinished)
FROM orders
GROUP BY Customer
HAVING sum(DateFinished > CURDATE() - interval 180 day) = 0
答案 1 :(得分:0)
try like this
SELECT DISTINCT orders.Customer, orders.DateFinished FROM orders
WHERE DATEDIFF(CURDATE(), Max(orders.DateFinished)) > 180
GROUP BY orders.Customer,orders.DateFinished
OR
select DISTINCT orders.Customer, orders.DateFinished
from orders
where orders.DateFinished >= date_sub(orders.DateFinished, interval 6 month);