Query editor
withreferral_list as (selectrow_number() over (partition by participant order by evt_block_time desc) as seqnum,referrer,participant,evt_block_timefrom elephant_bnb.ReferralData_evt_onReferralUpdate),leaderboard_referrer as (selectreferrer,count(participant) as amountfrom referral_listwhere seqnum = 1group by 1),leaderboard_participant as (selectparticipant,count(referrer) over (partition by participant) as amount,first_value(referrer) over (partition by participant order by seqnum ) as last_referrerfrom referral_list),prep as (selectdistinctparticipant,amount,last_referrerfrom leaderboard_participantorder by amount desc)selectrow_number() over (order by amount desc) as ranking,CONCAT('<a href="https://debank.com/profile/0', SUBSTRING(cast(participant as varchar), 2, 42), '?t=1696409060247&r=34381" target="_blank" rel="noopener noreferrer">0', SUBSTRING(cast(participant as varchar), 2, 5), '...',SUBSTRING(cast(participant as varchar), 39, 42), '</a>')AS wallet,participant,amount-1 as amount,CONCAT('<a href="https://debank.com/profile/0', SUBSTRING(cast(last_referrer as varchar), 2, 42), '?t=1696409060247&r=34381" target="_blank" rel="noopener noreferrer">0', SUBSTRING(cast(last_referrer as varchar), 2, 5), '...',SUBSTRING(cast(last_referrer as varchar), 39, 42), '</a>')AS wallet_last_referrer,last_referrer,count(*) over () as number_participantsfrom preporder by ranking