Query editor
WITH e AS (SELECT * FROM dune.tusa3112.result_rh_gmgn_4wallet_component_fifo),event_tx AS (SELECT source_period,tx_hash,MIN(wallet) wallet,MIN(block_time) block_time,MIN(day) day,MIN(block_number) block_number,MIN(tx_index) tx_index,BOOL_AND(success) success,MAX_BY(target,block_time) target,MAX_BY(selector,block_time) selector,MAX(token_rows) token_rows,SUM(assigned_tx_cash_usd) tx_cash_usd,SUM(assigned_gas_usd) gas_usd,SUM(buy_cost_usd) buy_cost_usd,SUM(paid_attributed_proceeds_usd) paid_proceeds_usd,SUM(nonpaid_attributed_proceeds_usd) nonpaid_proceeds_usd,SUM(unmatched_attributed_proceeds_usd) unmatched_proceeds_usd,SUM(opening_paid_basis_removed_usd) opening_basis_removed_usd,SUM(internal_token_qty) internal_token_qty,SUM(assigned_internal_quote_eth) internal_quote_eth,SUM(assigned_internal_usdg) internal_usdgFROM e WHERE row_kind='EVENT'GROUP BY 1,2),daily AS (SELECT source_period,day,COUNT(*) attempts,COUNT_IF(NOT success) reverts,COUNT(DISTINCT wallet) active_wallets,SUM(gas_usd) gas_usd,SUM(paid_proceeds_usd) paid_proceeds_usd,SUM(buy_cost_usd) buy_cost_usd,SUM(nonpaid_proceeds_usd) nonpaid_proceeds_usd,SUM(unmatched_proceeds_usd) unmatched_proceeds_usd,SUM(opening_basis_removed_usd) opening_basis_removed_usd,SUM(paid_proceeds_usd)-SUM(buy_cost_usd)-SUM(gas_usd)-IF(source_period='OOS',SUM(opening_basis_removed_usd),0e0) strict_zero_mark_net_usdFROM event_tx GROUP BY 1,2),hist_stats AS (SELECT COUNT(*) hist_active_days,COUNT_IF(strict_zero_mark_net_usd>=500e0) hist_days_ge_500,COUNT_IF(strict_zero_mark_net_usd>0e0) hist_profitable_days,SUM(strict_zero_mark_net_usd) hist_strict_total_usd,AVG(strict_zero_mark_net_usd) hist_avg_day_usd,APPROX_PERCENTILE(strict_zero_mark_net_usd,0.5) hist_median_day_usd,MIN(strict_zero_mark_net_usd) hist_worst_day_usd,MAX(strict_zero_mark_net_usd) hist_best_day_usd,SUM(gas_usd) hist_gas_usd,SUM(attempts) hist_attempts,SUM(reverts) hist_revertsFROM daily WHERE source_period='HIST'),oos_stats AS (SELECT MIN(day) oos_day,SUM(attempts) oos_attempts,SUM(reverts) oos_reverts,MAX(active_wallets) oos_active_wallets,SUM(gas_usd) oos_gas_usd,SUM(paid_proceeds_usd) oos_paid_proceeds_usd,SUM(buy_cost_usd) oos_buy_cost_usd,SUM(opening_basis_removed_usd) oos_opening_basis_removed_usd,SUM(nonpaid_proceeds_usd) oos_nonpaid_proceeds_usd,SUM(unmatched_proceeds_usd) oos_unmatched_proceeds_usd,SUM(strict_zero_mark_net_usd) oos_strict_zero_mark_net_usdFROM daily WHERE source_period='OOS'),inventory_token AS (SELECT token,MAX_BY(symbol,block_time) symbol,SUM(IF(lot_kind='PAID',acquired_qty,0e0)) paid_acquired_qty,SUM(IF(lot_kind IN ('UNPAID_SELF','EXTERNAL'),acquired_qty,0e0)) nonpaid_acquired_qty,SUM(paid_removed_qty) paid_removed_qty,SUM(nonpaid_removed_qty) nonpaid_removed_qty,SUM(acquired_cost_usd)-SUM(removed_paid_basis_usd) current_open_paid_basis_usd,SUM(IF(source_period='HIST',acquired_cost_usd,0e0))-SUM(IF(source_period='HIST',removed_paid_basis_usd,0e0)) opening_paid_basis_usd