with table1 as (
select mindate::date as day,
count (distinct from_address) as New_Users_Count,
sum (new_users_count) over (order by day) as Total_Users
from (select from_address, min (block_timestamp) as mindate from gnosis.core.fact_transactions group by 1)
group by 1)
select block_timestamp::date as date,
new_users_count,
Total_Users,
count (distinct tx_hash) as TX_Count,
count (distinct from_address) as Users_Count,
sum (tx_fee) as TX_Fee,
avg (TX_Fee) as Average_Fee
from gnosis.core.fact_transactions t1 join table1 t2 on t1.block_timestamp::date = t2.day
where status = 'SUCCESS'
and block_timestamp >= CURRENT_DATE - 40
group by 1,2,3
order by 1