当另一行为空时重复一行的值

repeat value of a row when other row is null

我有一个 table 这样的:

 id | nu_cns | nu_cpf | co_dim_tempo | sifilis | hiv
908 |   null |    347 |            1 |       y |   n
908 |    708 |   null |            2 |       y |   y
908 |    708 |   null |            3 |       y |   y

我需要重复 nu_cnsnu_cpf 字段的值,当这些字段是 null 时,例如:

 id | nu_cns | nu_cpf | co_dim_tempo | sifilis | hiv
908 |   *708 |    347 |            1 |       y |   n
908 |    708 |   *347 |            2 |       y |   y
908 |    708 |   *347 |            3 |       y |   y

您可以将 last_value 与框架子句一起使用 rows between unbounded preceding and current row:

select id, first_value(nu_cns) 
  over (partition by id order by nu_cns rows between unbounded preceding and current row)
  from my_table

如果你对任何值都满意,就做..

select *, 
       max(nu_cns) over (partition by id),
       max(nu_cpf) over (partition by id)  
from t;