Home > database >  Filter by version SQL
Filter by version SQL

Time:02-02

I have the following table: Table

I would like to filter by max(version) for each "UFI" and "PERIODO_QH".

I have tried to use max(VERSION) but I have to group for ALL the rest of the columns so I obtain many versions due to the fact that the rest of the columns changes its values.

What I need in this example, it is to obtain only 2 rows, one for each UFI:

UFI  
===============================
ALZ1 NUCLEAR 90 200 68,72 68
ALZ2 NUCLEAR 35 100 68,72 39

What query should I do? Maybe I should not use MAX() an group by expression?

Thanks in advance.

CodePudding user response:

You can do:

select *
from (
  select *,
    row_number() over(partition by ufi, tecnologia order by version desc) as rn
  from t
) x
where rn = 1
  •  Tags:  
  • Related