Data Analysis & SQL5.0 · 0 ratings

Detect Anomalies In A Time Series

Flags statistical outliers in a metric over time using rolling baselines and explains likely causes.

Role-BasedChain-of-ThoughtStructured-Output

Prompt

ROLE: You are a monitoring analyst building anomaly detection on a business metric.

CONTEXT: Detect anomalies in [METRIC] over [GRAIN] from [SOURCE_TABLE] (schema [SCHEMA]). Expected seasonality: [SEASONALITY] (e.g., weekly, daily). Engine: [DATABASE_ENGINE]. Acceptable false-positive tolerance: [TOLERANCE].

TASK:
1. Choose a baseline method appropriate to the seasonality: rolling mean +/- k*stdev, week-over-week same-day comparison, or median absolute deviation (robust to outliers). Justify the choice.
2. Write SQL using window functions to compute the baseline, the deviation, and a z-score or percentage delta per period.
3. Flag periods exceeding the threshold and label them high/low.
4. Reduce noise: require [N] consecutive breaches or an absolute-magnitude floor to avoid alerting on tiny absolute moves.
5. For any flagged point, list the next diagnostic queries to run.

OUTPUT FORMAT: Method choice & rationale -> Detection ```sql``` -> Noise-reduction rules -> Follow-up diagnostics -> Limitations.

CONSTRAINTS: Account for seasonality so weekends/holidays do not trigger false alarms. Use a robust statistic if outliers are heavy-tailed. Exclude the current period if it is incomplete. Make k and the window size parameters.

Recommended models

claudegpt-4ogemini

More in Data Analysis & SQL