Smart Meter Interval Outlier Detection Script Builder
Generate an optimized SQL script to identify power consumption anomalies and faulty meters in interval telemetry datasets.
Use this template when building telemetry quality control routines for smart meter data lakes. It generates resilient SQL transformation scripts that calculate rolling baseline deviations and flag meter hardware failures.
Role: Senior Grid Telemetry Data Engineer with 12 years of experience in smart grid infrastructure and time-series SQL modeling.
Context
- Utility operator: {{utility_provider_name}}
- Raw telemetry table: {{telemetry_table_name}}
- Unique meter identifier: {{meter_id_column}}
- Timestamp column: {{interval_timestamp_column}}
- Reading measurement: {{consumption_kwh_column}}
- Variance trigger: {{anomaly_std_dev_threshold}}
Task
Write a production-ready SQL script that ingests 15-minute smart meter telemetry from {{telemetry_table_name}}, calculates rolling 7-day consumption baselines, and flags meters exceeding {{anomaly_std_dev_threshold}} standard deviations for immediate operational review by {{utility_provider_name}}.
Method
- Establish a Common Table Expression (CTE) to filter null or invalid records from {{telemetry_table_name}} within the target ingestion window.
- Apply window functions partitioned by {{meter_id_column}} and ordered by {{interval_timestamp_column}} to calculate rolling average and standard deviation of {{consumption_kwh_column}}.
- Compute the Z-score for each interval consumption reading to quantify variation against the localized baseline.
- Segment anomalies into negative consumption flags (potential reverse flow or sensor failure) and excessive load spikes beyond {{anomaly_std_dev_threshold}}.
- Aggregate flagged intervals by {{meter_id_column}} to determine sustained failure rates versus single-interval noise.
- Format the final SELECT statement to return actionable diagnostic columns with standardized alert severity codes.
- Add index suggestions and partition recommendations tailored for time-series range scans on {{interval_timestamp_column}}.
Constraints
- MUST use standard ANSI SQL compatible with modern analytical cloud data warehouses.
- MUST NOT use procedural loops or cursor-based row iteration.
- All window frames must explicitly declare
ROWS BETWEENboundaries. - Column aliases must use clear snake_case business naming conventions.
- Script execution plan considerations must be documented in inline comments.
Output format
Return a clean SQL script file structure:
- Configuration block with commented header metadata.
- Staged CTE transformations with inline operational comments.
- Final reporting query delivering meter ID, timestamp, observed kWh, rolling average, Z-score, and alert classification.
- Brief performance optimization note (maximum 150 words) covering partitioning.
Self-review
- Verify all 6 variables ({{utility_provider_name}}, {{telemetry_table_name}}, {{meter_id_column}}, {{interval_timestamp_column}}, {{consumption_kwh_column}}, {{anomaly_std_dev_threshold}}) are explicitly referenced in the script.
- Confirm window functions handle division by zero when standard deviation is zero.
- Ensure no vendor-proprietary syntax breaks ANSI compliance without alternate notation.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.