PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

PostgreSQL-Fensterfunktionen ignorieren Nullwerte

Ich nehme an, Sie können nicht einfach:

select
from tbl_x
window w as (partition by ID order by Date)
where col_a is null;

Wenn nicht, müssen Sie möglicherweise:

select
Col_A,
Col_B,
case when Col_A is null
  then (select col_a
          from tbl_x x2
         where x2.id = x1.id
           and col_a is not null
           and x2.date < x1.date
      order by date desc
         limit 1)
  else Col_A
  end Col_A_Lag,
case when Col_B is null
  then (select col_b
          from tbl_x x2
         where x2.id = x1.id
           and col_b is not null
           and x2.date < x1.date
      order by date desc
         limit 1)
  else Col_B
  end Col_B_Lag
from tbl_x x1;

Bei entsprechender Indizierung wäre die Performance wahrscheinlich ziemlich gut.

create index ... (id, date desc, col_b) where col_b is not null;