Query editor
--7723158WITH builder_trades AS (SELECT *FROM query_7723158),resolved_markets AS (SELECTLOWER(condition_id) AS condition_id_hex,MAX_BY(outcome, resolved_on_timestamp) AS winning_outcome_name,MAX(resolved_on_timestamp) AS resolved_on_timestampFROM polymarket_polygon.market_detailsWHERE resolved_on_timestamp IS NOT NULLAND outcome IS NOT NULLGROUP BY 1),settled_trade_pnl AS (SELECTt.day,t.user_address,t.condition_id_hex,t.token_outcome_name,r.winning_outcome_name,SUM(t.volume_usd) AS settled_volume_usd,SUM(t.fee_usd) AS settled_fees_usd,SUM(t.buy_volume_usd) AS settled_buy_volume_usd,SUM(t.sell_volume_usd) AS settled_sell_volume_usd,SUM(t.signed_shares) AS net_shares,SUM(t.trading_cashflow_usd) AS trading_cashflow_usdFROM builder_trades tINNER JOIN resolved_markets rON LOWER(t.condition_id_hex) = LOWER(r.condition_id_hex)GROUP BY 1, 2, 3, 4, 5),settled_daily_pnl AS (SELECTday,SUM(settled_volume_usd) AS settled_volume_usd,SUM(settled_fees_usd) AS settled_fees_usd,SUM(settled_buy_volume_usd) AS settled_buy_volume_usd,SUM(settled_sell_volume_usd) AS settled_sell_volume_usd,SUM(CASEWHEN token_outcome_name = winning_outcome_name THEN net_sharesELSE 0END) AS settlement_payout_usd,SUM(trading_cashflow_usd+CASEWHEN token_outcome_name = winning_outcome_name THEN net_shares