Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 24, 2022 15:23
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 mshakhomirov/f90b035ba259e672d4d51a669e0cd1fc to your computer and use it in GitHub Desktop.
Save mshakhomirov/f90b035ba259e672d4d51a669e0cd1fc to your computer and use it in GitHub Desktop.
select * from
(
-- #1 from_item
select
extract(month from dt) as mo
,product_type
,revenue
from (
select
date(current_date()) as dt
, 'premium_account' as product_type
, 100 as revenue
union all
select
date_sub(current_date(), interval 1 month) as dt
, 'premium_group' as product_type
, 200 as revenue
union all
select
date_sub(current_date(), interval 2 month) as dt
, 'bots' as product_type
, 300 as revenue
)
)
pivot
(
-- #2 aggregate
avg(revenue) as avg_revenue_
-- #3 pivot_column
for product_type in ('premium_account', 'premium_group')
)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment