SELECT * from
(SELECT *,
ROW_NUMBER() over (partition by day order by avg_txs desc) as rn
from
(SELECT date(block_timestamp) as day, token_in, count(DISTINCT tx_hash) as total_txs,
sum(amount_out) as total_amt,
avg(total_txs) over (partition by token_in order by day) as avg_txs
from near.core.ez_dex_swaps
where token_out ilike 'wnear'
GROUP by 1,2 ))
where rn < 4