user1900054 user1900054 - 10 days ago 6
MySQL Question

selecting distinct value order by desc

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

Answer

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
Comments