Query editor
WITH-- Step 1: Calculate daily metrics for last 60 daysdaily_metrics AS (SELECTdate,COUNT(*) as total_transactions,COUNT(DISTINCT sender) as unique_users,CAST(COUNT(*) AS DOUBLE) / NULLIF(COUNT(DISTINCT sender), 0) as tx_per_userFROM sui.transactionsWHERE date >= CURRENT_DATE - INTERVAL '60' DAYAND execution_success = trueGROUP BY date),-- Step 2: Calculate baseline statistics (mean and standard deviation)stats AS (SELECTAVG(tx_per_user) as mean_ratio,STDDEV(tx_per_user) as stddev_ratioFROM daily_metrics)-- Step 3: Identify anomaly dates and calculate deviation metricsSELECTdm.date,dm.total_transactions,dm.unique_users,ROUND(dm.tx_per_user, 2) as tx_per_user,ROUND(s.mean_ratio, 2) as baseline_avg,ROUND((dm.tx_per_user - s.mean_ratio) / NULLIF(s.stddev_ratio, 0), 2) as z_score,ROUND(((dm.tx_per_user - s.mean_ratio) / NULLIF(s.mean_ratio, 0)) * 100, 1) as pct_deviationFROM daily_metrics dmCROSS JOIN stats sWHERE ABS((dm.tx_per_user - s.mean_ratio) / NULLIF(s.stddev_ratio, 0)) > 1.5ORDER BY ABS((dm.tx_per_user - s.mean_ratio) / NULLIF(s.stddev_ratio, 0)) DESC