Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, sorting before a group by

Tags:

sql

mysql

If I issue a query like this:

select c1, c2, c3
from table
group by c1;

i get distinct results for c1, but how do i sort it (e.g. c2 descending) before the group by?

like image 388
metafex Avatar asked Mar 11 '26 13:03

metafex


2 Answers

select c1, c2, c3 
from (select c1, c2, c3 from table order by c2 desc) t 
group by c1;
like image 134
Vitalii Fedorenko Avatar answered Mar 14 '26 07:03

Vitalii Fedorenko


Your question is unclear, but if you need got higher value of c2 for each c1 you may use Max

select c1, Max(c2), Max(c3)
from table
group by c1
like image 37
Michael Pakhantsov Avatar answered Mar 14 '26 07:03

Michael Pakhantsov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!