我的数据库中有这些表:
id Name Order Date
1 A 29-Dec
2 B 15-Dec
3 C 12-Oct
4 A 15-Dec
5 B 15-Sep
6 A 15-Sep
7 B 15-Aug
8 C 15-Aug
9 A 15-Jul
预期产出: -
Name QTY Last Order Date
A 4 29-Dec
B 3 15-Dec
C 2 12-Oct
如何做到这一点,请帮助。
此外,我必须只选择最后订单日期大于10天的记录。
此查询背后的目的是向此类客户发送自10天以来未订购的通知。怎么做,请帮忙......
答案 0 :(得分:1)
试试这个:
SELECT name, count(name), MAX(date) as latest_order_date
FROM Orders
GROUP BY name
ORDER BY name ASC
如果您对如何仅选择最后订单日期超过10天的记录的评论,可以使用HAVING和DATEDIFF
SELECT name, count(name), MAX(date) as latest_order_date
FROM Orders
GROUP BY name
HAVING DATEDIFF(NOW(),latest_order_date) > 10
ORDER BY name ASC
答案 1 :(得分:0)
请检查此...字段和表名称不同,请相应更改
select name, count(name), date from abc group by name order by date desc