After php mysql grouping, take the first data and the last data in the group and do statistics. How to write MYSQL like this?

you need to count the sum of price and number by time grouping and take out the values of the first price and the last price in the group
. I haven"t been able to find the corresponding method for a long time. I hope you can guide me

.
Mar.29,2021

time grouping. The sorting field is not clear. Let's sort it by price

.
select 
    mtime,sum(price),sum(number),
    substring_index(group_concat(price order by price),',',1) min_price,
    substring_index(group_concat(price order by price),',',-1) max_price
from table group by mtime;

select min(price) as min_price, max(price) as max_price from (select mtime, code, sum(price) as price, sum(number) as number from table_xx group by mtime order by mtime desc) data
Menu