-- Monthly summary of ALEX protocol performance
WITH monthly_summary AS (
SELECT
DATE_TRUNC('month', date) as month,
AVG(tvl) as avg_tvl,
MAX(tvl) as peak_tvl,
MIN(tvl) as min_tvl,
AVG(price) as avg_price,
AVG(market_cap) as avg_market_cap,
SUM(token_volume) as total_token_volume,
AVG(token_turnover_circulating) as avg_daily_turnover
FROM
art_share.alex.ez_metrics
WHERE
date >= DATEADD(month, -12, CURRENT_DATE())
GROUP BY
DATE_TRUNC('month', date)
)
SELECT
month,
avg_tvl,
peak_tvl,
avg_price,
avg_market_cap,
total_token_volume,
avg_daily_turnover * 30 as estimated_monthly_turnover,
-- Month-over-month growth
(avg_tvl - LAG(avg_tvl, 1) OVER (ORDER BY month)) / NULLIF(LAG(avg_tvl, 1) OVER (ORDER BY month), 0) * 100 as tvl_growth_mom,
(avg_price - LAG(avg_price, 1) OVER (ORDER BY month)) / NULLIF(LAG(avg_price, 1) OVER (ORDER BY month), 0) * 100 as price_growth_mom
FROM
monthly_summary
ORDER BY
month DESC