Query editor
WITH weth_price AS (SELECT priceFROM prices.usd_latestWHERE blockchain = 'base'AND symbol = 'WETH'LIMIT 1),trade_classification AS (SELECTblock_time,tx_hash,CASEWHEN taker IN (0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad,0x743f2f29cdd66242fb27d292ab2cc92f45674635,0x11ddD59C33c73C44733b4123a86Ea5ce57F6e854) THEN tx_fromELSE takerEND as effective_trader,maker,CASEWHEN token_bought_symbol = 'WETH' THEN 'SELL'WHEN token_bought_symbol = 'CLANKER' THEN 'BUY'END as trade_type,-- Amount trackingCASEWHEN token_bought_symbol = 'CLANKER' THEN token_bought_amountWHEN token_sold_symbol = 'CLANKER' THEN token_sold_amountEND as clanker_amount,-- WETH amountCASEWHEN token_bought_symbol = 'WETH' THEN token_bought_amountWHEN token_sold_symbol = 'WETH' THEN token_sold_amountEND as weth_amount,-- USD values using current WETH priceCASEWHEN token_bought_symbol = 'WETH' THEN token_bought_amount * wp.priceWHEN token_sold_symbol = 'WETH' THEN token_sold_amount * wp.priceEND as trade_value_usdFROM dex.tradesCROSS JOIN weth_price wpWHERE blockchain = 'base'AND project = 'uniswap'AND block_month > timestamp '2024-10-01 00:00'AND project_contract_address = 0xc1a6fbedae68e1472dbb91fe29b51f7a0bd44f97),position_tracking AS (SELECT*,SUM(CASEWHEN trade_type = 'BUY' THEN clanker_amountWHEN trade_type = 'SELL' THEN -clanker_amountEND) OVER (PARTITION BY effective_trader ORDER BY block_time) as running_position,-- Running sum of WETH spent and CLANKER bought (for average cost calculation)SUM(CASE WHEN trade_type = 'BUY' THEN weth_amount ELSE 0 END)OVER (PARTITION BY effective_trader ORDER BY block_time) as running_weth_spent,SUM(CASE WHEN trade_type = 'BUY' THEN clanker_amount ELSE 0 END)OVER (PARTITION BY effective_trader ORDER BY block_time) as running_clanker_bought,-- Running USD valuesSUM(CASE WHEN trade_type = 'BUY' THEN trade_value_usd ELSE 0 END)OVER (PARTITION BY effective_trader ORDER BY block_time) as running_usd_spentFROM trade_classification