Skip to content

Instantly share code, notes, and snippets.

@Arqentum
Created May 6, 2025 19:37
Show Gist options
  • Select an option

  • Save Arqentum/7f72a16dc8cc945356bde1258619f3da to your computer and use it in GitHub Desktop.

Select an option

Save Arqentum/7f72a16dc8cc945356bde1258619f3da to your computer and use it in GitHub Desktop.
#BiqQuery #SQL back and forward fill
with tt1 as (select 1 as time, null as val union all
select 2, 1 union all
select 3, null union all
select 4, null union all
select 5, null union all
select 6, 0 union all
select 7, null union all
select 8, null
)
select *
, first_value(val ignore nulls) over (order by time) fv
, last_value(val ignore nulls) over (order by time asc) as fill_down
, last_value(val ignore nulls) over (order by time desc) as fill_down
, first_value(val ignore nulls) over (order by time RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) fv2
from tt1
where true
order by time
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment