Skip to content

Instantly share code, notes, and snippets.

@sh16ma
Last active January 19, 2022 10:09
Show Gist options
  • Save sh16ma/371f002770da8ceec597e7f817d15212 to your computer and use it in GitHub Desktop.
Save sh16ma/371f002770da8ceec597e7f817d15212 to your computer and use it in GitHub Desktop.
#🔎 #BigQuery #日数差 #ウィンドウ関数 #ウィンドウフレーム
#standaardSQL
select
Location
, datetime_diff(new_date, old_date, day) as days_diff
from(
select
-- *
Date
, Location
, first_value(Date)over(
partition by Location
order by Date asc) as old_date
, last_value(Date)over(
partition by Location
order by Date asc
rows between UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING ) as new_date
from
`pj.ds.weatherAUS`
)
group by
Location
, days_diff
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment