Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 24, 2022 15:12
Show Gist options
  • Save mshakhomirov/ebf5488d0036bc9b84ae05889346d986 to your computer and use it in GitHub Desktop.
Save mshakhomirov/ebf5488d0036bc9b84ae05889346d986 to your computer and use it in GitHub Desktop.
with dates as (
select
dt
from 
unnest(generate_date_array(date_sub(current_date(), interval 90 day), current_date(), interval 1 day)) as dt
)
, data as (
select dt
, CEIL(RAND()*1000) as revenue -- just some random data.
from
dates
)
select
dt
, revenue
, AVG(revenue) OVER(ORDER BY unix_date(dt) RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) as seven_day_moving_average
from data
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment