Query editor
With Block AS (SelectUser,inflow,outflow,balance,holding_ratefrom query_4009542),inflow AS (SELECTday,"to" AS trader,contract_address AS token_address,SUM(amount_usd) as inflow,SUM(amount) as inflow_rawFROM dune.brightspear.result_eth_token_flowsInner Join Block as B on "to"= B.userGROUP BY 2,3,1),outflow AS (SELECTday,"from" AS trader,contract_address AS token_address,SUM(amount_usd) as outflow,SUM(amount) as outflow_rawFROM dune.brightspear.result_eth_token_flowsInner Join Block as B on "from"= B.userGROUP BY 2,3,1),T1 AS (SELECTCOALESCE(A.trader, B.trader) AS trader,COALESCE(A.token_address, B.token_address) AS token_address,COALESCE(A.day, B.day) AS day,COALESCE(A.inflow, 0) AS inflow,COALESCE(B.outflow, 0) AS outflow,COALESCE(A.inflow_raw, 0) AS inflow_raw,COALESCE(B.outflow_raw, 0) AS outflow_rawFROM inflow AS AFULL JOIN outflow AS BON A.token_address = B.token_address AND A.trader = B.trader AND A.day = B.day),hourly_balance AS (SELECTtrader,A.token_address AS token_address,A.day AS day,inflow,outflow,inflow_raw,outflow_raw,inflow_raw - outflow_raw as current_balance,SUM(inflow) OVER (PARTITION BY trader, A.token_address ORDER BY A.day)- SUM(outflow) OVER (PARTITION BY trader, A.token_address ORDER BY A.day) AS run_balance_usd,EXTRACT(day FROM LEAD(A.day, 1, CURRENT_TIMESTAMP) OVER (PARTITION BY trader, A.token_address ORDER BY A.day) - A.day) AS durationFROM T1 as A