Search code examples
mariadbgroupwise-maximum

MariaDB group by date order by date desc not working


I have a table like this:

date
2020-03-11 10:41:26
2020-03-11 17:51:15
2020-03-12 10:45:28
2020-03-12 17:41:28

I want the result:

datech        date                cnt
2020年3月12日  2020-03-12 17:41:28 2
2020年3月11日  2020-03-11 17:51:15 2

I have tried

Select *, count(*) as cnt from (
  Select (DATE_FORMAT((date),'%Y年%c月%d日')) as datech, date
  from tablea where 1=1
  order by date desc
) as t         
GROUP BY datech order by date desc

but the result date is asc

datech         date                cnt
2020年3月12日  2020-03-12 10:45:28 2
2020年3月11日  2020-03-11 10:41:26 2  

How can I desc date? Thanks.


Solution

  • Try this:

    Select datech, max(date) as date, count(*) as cnt from (
      Select (DATE_FORMAT((date),'%Y年%c月%d日')) as datech, date
      from tablea where 1=1
    ) as t         
    GROUP BY datech order by date desc;