I am using a Derby database.
I have a table like this:
TB_ORDERS
BUYER_NAME DATE_CREATED OTHER_COLLUMS ......
---------------------------------------------
DAVID 2012-09-01 ----
PETER 2012-09-14 ----
DAVID 2012-09-05 ----
PETER 2012-09-02 ----
DAVID 2012-08-15 ----
MARY 2012-09-02 ----
MARY 2012-09-15 ----
I am trying to get a result grouped by BUYER_NAME where each group should be ordered by DATE_CREATED and finally everything ordered by the group’s most recent date, like this:
MARY 2012-09-15
MARY 2012-09-12
PETER 2012-09-14
PETER 2012-09-02
DAVID 2012-09-05
DAVID 2012-09-01
DAVID 2012-08-15
As you can see, the Mary’s Group has the most recent date_created so it is placed on top.
Then we get Peter’s group on second place (note that Peter’s group has a date “09-14” higher then one date on Mary’s group “09-12” however, Peter’s group is placed after Mary’s group as every thing should be ordered by the group’s most recent date.
I have tried every thing I know with no success. The closest I got was:
Select date_created, buyer_name
From ORDERS
Order By buyer_name,date_created Desc;
However, the groups are not ordered by the most recent group date.
Should I do that in my code or is there a better way?
This query will bring you the most recent date_created of each buyer_name, together with the result set that you already had. If you include this new column in your Order By clause it should do the trick: