sql >> Base de Datos >  >> RDS >> PostgreSQL

seleccionando una columna basada en un valor mínimo de otra columna

En SQL estándar, esto se puede hacer usando una función de ventana

select test_type, model, firmware_version, avg_throughput
from (
  select test_type, model, firmware_version, avg_throughput, 
         min(firmware_version) over (partition by test_type, model) as min_firmware
  from temp_table
) t
where firmware_version = min_firmware;

Sin embargo, Postgres tiene el distinct on operador que suele ser más rápido que la solución correspondiente con una función de ventana:

select distinct on (test_type, model) 
       test_type, model, firmware_version, avg_throughput
from temp_table
order by test_type, model, firmware_version;

Ejemplo de SQLFiddle:http://sqlfiddle.com/#!15/563bd/1