select *,
row_number() over (order by total_fee desc) as rank
from
(select LENDING_POOL, sum(TX_FEE) as total_fee
from
(select a.BLOCK_TIMESTAMP, LENDING_POOL,TX_FEE,'Borrowing' as tag
from flipside_prod_db.crosschain.ez_borrowing a
left join polygon.core.fact_transactions b
on a.tx_hash=b.tx_hash
where BLOCKCHAIN='polygon'
union all
select a.BLOCK_TIMESTAMP, LENDING_POOL,TX_FEE,'Lending' as tag
from flipside_prod_db.crosschain.ez_lending a
left join polygon.core.fact_transactions b
on a.tx_hash=b.tx_hash
where BLOCKCHAIN='polygon')
group by LENDING_POOL
order by total_fee desc
limit 20)