select
date_trunc('month', e.block_timestamp) as date,
tx_receiver as contract,
count(e.tx_hash) as interaction_count,
row_number() over(partition by date order by interaction_count desc) as amount_rank,
case when amount_rank < 6 then tx_receiver else 'rest' end as contract_category
from near.core.fact_actions_events e, near.core.fact_transactions t
where
date >= '2022-01-01' and
e.tx_hash = t.tx_hash and
action_name = 'DeployContract'
group by 1, 2