Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the MAX date between a range

Tags:

sql

mysql

I was searching for querys but i cant find an answer that helps me or if exit a similar question.

i need to get the info of the customers that made their last purchase between two dates

+--------+------------+------------+
| client | amt        | date       |
+--------+------------+------------+
|      1 |  2440.9100 | 2014-02-05 |
|      1 | 21640.4600 | 2014-03-11 |
|      2 |  6782.5000 | 2014-03-12 |
|      2 |  1324.6600 | 2014-05-28 |
+--------+------------+------------+

for example if i want to know all the cust who make the last purchase between 2014-02-11 and 2014-03-16, in that case the result must be

+--------+------------+------------+
| client | amt        | date       |
+--------+------------+------------+
|      1 | 21640.4600 | 2014-03-11 |
+--------+------------+------------+

cant be the client number 2 cause have a purchease on 2014-05-28, i try to make a

SELECT MAX(date) 
FROM table 
GROUP BY client

but that only get the max of all dates, i dont know if exist a function or something that can help, thanks.


well i dont know how to mark this question as resolved but this work for me to complete the original query

SELECT client, MAX(date) 
FROM table 
GROUP BY  client
HAVING MAX(date) BETWEEN date1 AND date2

thanks to all that took a minute to help me with my problem, special thanks to Ollie Jones and Peter Pei Guo

like image 893
user3680275 Avatar asked Aug 25 '26 03:08

user3680275


1 Answers

Something in this format, replace date1 and date 2 with the real values.

SELECT client, max(date) 
from table 
group by client
having max(date) between date1 AND date2
like image 175
Peter Pei Guo Avatar answered Aug 27 '26 18:08

Peter Pei Guo