My table structure as follows
id parent last_date sub_type
----------------------------------
11 9 2017-02-28 1101
10 9 2016-08-26 1101
8 1 2017-02-20 1101
12 12 2016-08-31 1102
14 12 2016-12-20 1102
9 9 2016-12-31 1101
13 12 2017-03-23 1102
2 1 2017-01-25 1101
1 1 2016-12-31 1101
i want to fetch rows for each sub_type based the date (longest first) . i tried following query
SELECT * FROM mytable GROUP BY sub_type ORDER BY ISNULL(last_date) DESC, last_date DESC
and it results
id parent last_date sub_type
--------------------------------
1 1 2016-12-31 1101
12 12 2016-08-31 1102
But i expect below result .
id parent last_date sub_type
--------------------------------
13 12 2017-03-23 1102
11 9 2017-02-28 1101
Please guide me to get above result .
EDIT:
last_date may have NULL value which will max precedence over dated entries. Thatswhy i choose ISNULL DESC order.