Created
November 24, 2022 15:23
-
-
Save mshakhomirov/f90b035ba259e672d4d51a669e0cd1fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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