sql >> Base de Datos >  >> RDS >> Mysql

SQL:uso de GROUP BY y MAX en varias columnas

Puede obtener los mejores valores de attrib1 y luego unirse a los valores de attrib2 y obtener lo mejor de ellos para cada valor de attrib1:

select t2.catID, t2.attrib1, max(t2.attrib2)
from
(
  select catID, max(attrib1) as attrib1
  from test_table
  group by catID
) t1
inner join test_table t2 on t2.catID = t1.catID and t2.attrib1 = t1.attrib1
group by t2.catID, t2.attrib1