select * from
(select *,
row_number() over (partition by date order by tx_count desc) as rank
from
(select BLOCK_TIMESTAMP::date as date, EVENT_CONTRACT as contract, count(distinct TX_ID) as tx_count
from flow.core.fact_events
where BLOCK_TIMESTAMP>='2022-05-09' and TX_SUCCEEDED='TRUE'
group by date, contract))
where rank<=10