How to sort the result based on a certain column when using GROUP BY in sql statement I am new to this, so please be patient...


I have a table "tFruit".
The fields inside tFruit are "ID", "Buyer", "Seller", "Time", and "FruitName".
"ID" is the key field, the other 4 fiends has non-unique data.


When I use this SQL statement I will get all sellers sorted by Time. But an individual seller appears multiple times because they sold fruits not only once.

SELECT Seller 
FROM tFruit 
WHERE Buyer = 'email@address.com' 
ORDER BY Time DESC


What I want is to get the list of seller, non-duplicate, sorted by Time. Thus I use this statement:

SELECT Seller 
FROM tFruit 
WHERE Buyer = 'email@address.com' 
GROUP BY Seller 
ORDER BY Time DESC


Now there is no duplicate Seller, but they are not sorted by Time. 

Any ideas how to do this?

Thanks!
Try adding the Time field to your select list, as in:

SELECT Seller, Max(Time) AS MostRecentTime 
FROM tFruit 
WHERE Buyer = 'email@address.com' 
GROUP BY Seller 
ORDER BY Max(Time) DESC;


Max(Time) as MostRecentTime will display the most recent Time for the given Seller.  You don't really need this in the SELECT clause, you really only need MAX(Time) in the ORDER BY clause.  I've added it to the SELECT clause so you can see the results are sorted by Time in descending order.