Query editor
WITH deposit_usd AS (SELECTdate_trunc('day', date) AS day, -- Ensures the date is in 'day' granularityamount AS total_deposits_usd -- Already grouped by date, so just rename the columnFROM query_4199611),withdraw_usd AS (SELECTdate_trunc('day', date) AS day, -- Ensures the date is in 'day' granularityamount AS total_withdraws_usd -- Already grouped by date, so just rename the columnFROM query_4199696),-- Combine deposits and withdrawals for each daytvl_data AS (SELECTCOALESCE(d.day, w.day) AS day, -- Handle days with either deposit or withdrawalCOALESCE(d.total_deposits_usd, 0) AS total_deposits_usd,COALESCE(w.total_withdraws_usd, 0) AS total_withdraws_usdFROM deposit_usd dFULL OUTER JOIN withdraw_usd wON d.day = w.day),-- Generate cumulative TVLcumulative_tvl AS (SELECTday,SUM(total_deposits_usd - total_withdraws_usd) OVER (ORDER BY day) AS tvl_usd -- Cumulative TVLFROM tvl_data)-- Output daily timeseries of TVLSELECTday,tvl_usdFROM cumulative_tvlORDER BY day;