Query editor
WITH e AS (SELECT * FROM dune.tusa3112.result_rh_top100_provenance_eventsWHERE wallet=0xcb4d28c2106fe9f781615005ff87056d114135c9), t AS (SELECT token,MAX_BY(symbol,block_time) symbol,MIN(IF(event_class='PAID_BUY',block_time,NULL)) first_buy,MAX(IF(event_class='PAID_BUY',block_time,NULL)) last_buy,MIN(IF(event_class='CASH_SELL',block_time,NULL)) first_sell,MAX(IF(event_class='CASH_SELL',block_time,NULL)) last_sell,COUNT_IF(event_class='PAID_BUY') buy_txs,COUNT_IF(event_class='CASH_SELL') sell_txs,COUNT_IF(event_class='UNPAID_IN') unpaid_in_txs,COUNT_IF(event_class='UNPAID_OUT') unpaid_out_txs,SUM(IF(event_class='PAID_BUY',token_in,0e0)) paid_qty_in,SUM(IF(event_class='UNPAID_IN',token_in,0e0)) unpaid_qty_in,SUM(IF(event_class='CASH_SELL',token_out,0e0)) cash_qty_out,SUM(buy_cost_usd) observed_paid_cost_usd,SUM(proceeds_usd) observed_proceeds_usd,SUM(assigned_gas_usd) observed_token_tx_gas_usdFROM e WHERE token IS NOT NULLGROUP BY 1), x AS (SELECT *,paid_qty_in+unpaid_qty_in-cash_qty_out closing_qty,observed_proceeds_usd-observed_paid_cost_usd-observed_token_tx_gas_usd observed_zero_mark_contribution_usd,0.5348502693178402e0*(observed_proceeds_usd-observed_paid_cost_usd)-observed_token_tx_gas_usd frozen_zero_mark_contribution_usd,DATE_DIFF('second',first_buy,first_sell) first_exit_seconds,DATE_DIFF('second',first_buy,last_sell) full_exit_secondsFROM t)SELECT ROW_NUMBER() OVER(ORDER BY frozen_zero_mark_contribution_usd DESC) profit_rank,CONCAT('0x',TO_HEX(token)) token,symbol,first_buy,last_buy,first_sell,last_sell,buy_txs,sell_txs,unpaid_in_txs,unpaid_out_txs,paid_qty_in,unpaid_qty_in,cash_qty_out,closing_qty,observed_paid_cost_usd,observed_proceeds_usd,observed_token_tx_gas_usd,observed_zero_mark_contribution_usd,frozen_zero_mark_contribution_usd,first_exit_seconds,full_exit_secondsFROM xORDER BY frozen_zero_mark_contribution_usd DESC