Query editor
WITH stables AS (SELECT *FROM tokens.erc20_stablecoinsWHERE blockchain = 'polygon'),prep AS (SELECTdate_trunc('day', block_time) AS time,sum(case when to != 0x0000000000000000000000000000000000000000 and to!="from"then price_usd * amount end ) as volume_adj,SUM(amount) AS agg_amount,SUM(price_usd * amount) AS total_usd_amount,count(*) as cnt_txFROM tokens_polygon.transfers tJOIN stables sON s.contract_address = t.contract_address-- WHERE t.block_time >= DATE '2025-01-01'GROUP BY 1),base AS (SELECTtime,total_usd_amount,volume_adj,-- 30-day rolling transfer volume (USD)SUM(COALESCE(total_usd_amount, 0)) OVER (ORDER BY timeRANGE BETWEEN INTERVAL '29' DAY PRECEDING AND CURRENT ROW) AS total_vol_30d,SUM(COALESCE(cnt_tx, 0)) OVER (ORDER BY timeRANGE BETWEEN INTERVAL '29' DAY PRECEDING AND CURRENT ROW) AS total_tx_30d,-- All-time cumulativeSUM(COALESCE(total_usd_amount, 0)) OVER (ORDER BY time) AS total_vol,SUM(COALESCE(cnt_tx, 0)) OVER (ORDER BY time) AS total_txFROM prep)SELECTtime,total_usd_amount,total_vol_30d,total_tx_30d,total_vol,volume_adj,total_tx,-- Absolute 30-day change vs. value 30 rows (~days) agototal_vol_30d- LAG(total_vol_30d, 30) OVER (ORDER BY time) AS total_vol_30d_change,total_tx_30d- LAG(total_tx_30d, 30) OVER (ORDER BY time) AS total_tx_30d_change,-- Percent change vs. 30 rows (~days) agoCASEWHEN LAG(total_vol_30d, 30) OVER (ORDER BY time) IS NULLOR LAG(total_vol_30d, 30) OVER (ORDER BY time) = 0THEN NULLELSE (total_vol_30d / LAG(total_vol_30d, 30) OVER (ORDER BY time) - 1)*100END AS total_vol_30d_pct_change,