Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 24, 2022 14:44
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/4cf738aaad967fe92c4fb7192874fadf to your computer and use it in GitHub Desktop.
Save mshakhomirov/4cf738aaad967fe92c4fb7192874fadf to your computer and use it in GitHub Desktop.
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