with main as (
SELECT
min(t.BLOCK_TIMESTAMP) as time,TX_RECEIVER
FROM( near.core.fact_actions_events INNER JOIN near.core.fact_transactions t on t.TX_HASH = near.core.fact_actions_events.TX_HASH and t.BLOCK_TIMESTAMP=near.core.fact_actions_events.BLOCK_TIMESTAMP)
WHERE action_name = 'DeployContract'
GROUP BY TX_RECEIVER),
s_m as (
SELECT time,TX_RECEIVER as smart_contracts
from main
WHERE time>=CURRENT_DATE-30)
SELECT TX_RECEIVER,sum(GAS_USED)/pow(10,12) as gas
from (s_m INNER join near.core.fact_transactions t on s_m.smart_contracts=t.TX_RECEIVER)
GROUP BY TX_RECEIVER
order by gas desc
LIMIT 5