Skip to content

Instantly share code, notes, and snippets.

@nuria
Created February 5, 2023 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuria/1c85b22eae99fcc31b297fa77f6c3658 to your computer and use it in GitHub Desktop.
Save nuria/1c85b22eae99fcc31b297fa77f6c3658 to your computer and use it in GitHub Desktop.
avg and sum as window functions
with base_data as (
--
select date_trunc('day', received_at) as received_at, count(*) as events_received
from some_com.performance_largest_contentful_paint where received_at > '2022-01-01'
group by date_trunc('day', received_at) order by date_trunc('day', received_at)
)
select received_at, events_received ,
avg(events_received) over (partition by date_trunc('month',received_at)) as monthly_avg ,
max(events_received) over (partition by date_trunc('month', received_at)) as monthly_max
from base_data order by received_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment