SQL
AuraScore 81/100

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.

Template

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

  1. Establish a Common Table Expression (CTE) to filter null or invalid records from {{telemetry_table_name}} within the target ingestion window.
  2. 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}}.
  3. Compute the Z-score for each interval consumption reading to quantify variation against the localized baseline.
  4. Segment anomalies into negative consumption flags (potential reverse flow or sensor failure) and excessive load spikes beyond {{anomaly_std_dev_threshold}}.
  5. Aggregate flagged intervals by {{meter_id_column}} to determine sustained failure rates versus single-interval noise.
  6. Format the final SELECT statement to return actionable diagnostic columns with standardized alert severity codes.
  7. 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 BETWEEN boundaries.
  • 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:

  1. Configuration block with commented header metadata.
  2. Staged CTE transformations with inline operational comments.
  3. Final reporting query delivering meter ID, timestamp, observed kWh, rolling average, Z-score, and alert classification.
  4. 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.
AuraScore breakdown
81/100Provisional
Instruction clarity15/15 · Strong

Explicit role, a named task, and discrete steps the model can follow.

Context architecture12/12 · Strong

Background, inputs and variables the model needs before it starts.

Constraint engineering12/12 · Strong

Hard boundaries — what the model must and must not do.

Output specification6/14 · Thin

A named, field-level shape for the response.

Reasoning structure10/10 · Strong

Ordered work items that force analysis before an answer.

Model compatibility10/10 · Strong

Length and structure that travel across frontier models.

Token efficiency5/10 · Thin

Signal density — instruction weight without padding.

Reusability7/7 · Strong

Documented variables so the scaffold adapts to new inputs.

Robustness3/5 · Adequate

Quality bar, assumptions and behaviour when inputs are thin.

Observed performance1/5 · Thin

How much real usage the template has behind it.

data-analytics
data-sql
energy-utilities
smart-grid
telemetry
sql-analytics