-2

I have these table in my database:

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

Expected Output:-

Name    QTY     Last Order Date
A       4       29-Dec
B       3       15-Dec
C       2       12-Oct

How to do this, Please help.

Also I have to select only those record whose last order date is greater than 10 days.

The purpose behind this query is to send such customer a notification who are not ordering since 10 days. How to do this, Please help...

Saleem Jafar
  • 123
  • 1
  • 3
  • 13

2 Answers2

1

Try this :

SELECT name, count(name), MAX(date) as latest_order_date
FROM Orders 
GROUP BY name 
ORDER BY name ASC 

For your comment about how to select only those record where last order date is greater than 10 days, you can use HAVING and 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 
Cédric Miachon
  • 344
  • 1
  • 8
0

Please check this... the field and table names are different, please change it accordingly

select name, count(name), date from abc group by name order by date desc