selecting a column based on a minimum value of another column

前端 未结 3 1100
[愿得一人]
[愿得一人] 2021-01-13 08:46

I have the following table.

test_type |  brand  | model  | band | firmware_version | avg_throughput
-----------+---------+--------+------+-----------------+-         


        
3条回答
  •  孤街浪徒
    2021-01-13 09:21

    In standard SQL this can be done using a window function

    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;
    

    Postgres however has the distinct on operator which is usually faster than the corresponding solution with a window function:

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

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

提交回复
热议问题