Created
November 24, 2022 14:44
-
-
Save mshakhomirov/4cf738aaad967fe92c4fb7192874fadf 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
with data as ( | |
select | |
current_timestamp() as ts | |
,'stage' as context_type | |
,1 as user_id | |
,100 as credit_value | |
, true as is_gift | |
union all | |
select | |
timestamp_sub(current_timestamp(), interval 24 hour) as ts | |
,'user' as context_type | |
,1 as user_id | |
,200 as credit_value | |
,false as is_gift | |
union all | |
select | |
timestamp_sub(current_timestamp(), interval 24*2 hour) as ts | |
,'user' as context_type | |
,3 as user_id | |
,300 as credit_value | |
,true as is_gift | |
) | |
, results as ( | |
select | |
date(ts) as date | |
,context_type | |
,sum(credit_value)/100 as daily_credits_spend | |
from data | |
group by rollup(1, context_type) | |
order by 1 | |
) | |
select | |
date | |
,if(context_type is null, 'total', context_type) as context_type | |
,daily_credits_spend | |
from results | |
order by date | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment