Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting distinct value order by desc

Tags:

sql

mysql

i want to select distinct customer_id order by either s.no or time or anyhow

s.no     customer_id         time
1        3                   100001
2        2                   100002
3        4                   100003
4        3                   100004
5        2                   100005

i am using

select distinct(customer_id) from table_name order by time DESC

and it is giving the answer as 4 2 3 but as i want it should be 2 3 4

like image 523
user1900054 Avatar asked Dec 05 '22 09:12

user1900054


1 Answers

So your problem statement is "You want the list of customer_id, sorted in descended order from their largest time value", right?

SELECT customer_id, MAX(time)
FROM table_name
GROUP BY customer_id
ORDER BY MAX(time) DESC
like image 152
AgRizzo Avatar answered Dec 21 '22 06:12

AgRizzo