Query editor
-- $PRINT Sale: Top USDC investors leaderboardWITH sale_txs AS (SELECT DISTINCT tx_idFROM solana.account_activityWHERE address = '{{sale_contract}}'AND block_time >= TIMESTAMP '{{start_time}}'),transfers_raw AS (SELECTt.block_time,t.block_date,t.tx_id,t.tx_signer,t.from_owner,t.amount_display AS usdc_amount,CASE WHEN t.tx_signer = t.from_owner THEN 'Investment' ELSE 'Non-Investment' END AS tagFROM tokens_solana.transfers tINNER JOIN sale_txs s ON t.tx_id = s.tx_idWHERE t.token_mint_address = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'AND t.block_date >= CAST(TIMESTAMP '{{start_time}}' AS DATE)AND t.block_time >= TIMESTAMP '{{start_time}}'),investments AS (SELECTtx_id,tx_signer AS investor,MIN(block_time) AS block_time,SUM(usdc_amount) AS usdc_amountFROM transfers_rawWHERE tag = 'Investment'GROUP BY 1, 2),agg AS (SELECTinvestor,SUM(usdc_amount) AS total_usdc,COUNT(*) AS deposit_count,MIN(block_time) AS first_deposit,MAX(block_time) AS last_deposit,MIN(usdc_amount) AS min_deposit,MAX(usdc_amount) AS max_deposit,AVG(usdc_amount) AS avg_depositFROM investmentsGROUP BY 1)SELECTROW_NUMBER() OVER (ORDER BY total_usdc DESC) AS rank,investor,total_usdc,deposit_count,avg_deposit,min_deposit,max_deposit,first_deposit,last_deposit,100.0 * total_usdc / SUM(total_usdc) OVER () AS pct_of_totalFROM aggORDER BY total_usdc DESCLIMIT {{top_n}}