Workforce Shift Rollup and Anomaly Flagging Script
Create an automated database aggregation script to transform raw facility access logs into workforce shift summaries.
Use this template when HR Operations needs to transform physical badge-in data and employee master rosters into aggregated shift metrics, identifying overtime and attendance discrepancies.
Role: Lead People Analytics Data Engineer specializing in operational HR workforce data pipelines.
Context
- Analytics data warehouse: {{warehouse_platform}}
- Raw facility access log table: {{badge_event_table}}
- Canonical employee roster table: {{roster_table}}
- Aggregation interval: {{aggregation_granularity}}
- Weekly overtime ceiling in hours: {{overtime_threshold_hours}}
- Destination analytics mart table: {{output_mart_table}}
Task
Author an idempotent SQL rollup and anomaly detection script that transforms high-frequency access events from {{badge_event_table}}, enriches them with worker metadata from {{roster_table}}, aggregates them at the {{aggregation_granularity}} level, and writes daily shift totals and overtime warnings exceeding {{overtime_threshold_hours}} hours into {{output_mart_table}}.
Method
- Define Common Table Expressions (CTEs) to deduplicate raw badge taps from {{badge_event_table}} based on device timestamp.
- Pair alternating badge-in and badge-out events using window functions to calculate precise shift durations.
- Handle unmatched single-tap events by applying parameterized heuristic fallback shift boundaries.
- Join calculated shift sessions with department and contract metadata from {{roster_table}}.
- Roll up total hours, break times, and night shifts according to {{aggregation_granularity}}.
- Compute conditional indicator flags for any worker exceeding {{overtime_threshold_hours}} within the rolling cycle.
- Write an atomic MERGE / UPSERT statement targeting {{output_mart_table}} to guarantee idempotence during backfills.
Constraints
- MUST write ANSI-standard SQL optimized natively for {{warehouse_platform}} dialect syntax.
- MUST NOT cause duplicate shift entries when run multiple times against overlapping date ranges.
- All null badge-out scenarios must be explicitly flagged with an anomaly code.
- Calculations for {{overtime_threshold_hours}} must respect local timezone offsets defined in the roster.
Output format
- Section 1: Pipeline Architecture & Windowing Strategy (1 short paragraph).
- Section 2: Complete Executable SQL / MERGE Script (cleanly formatted, ready for scheduling).
- Section 3: Anomaly & Orphan Tap Detection Queries (2 monitoring SQL queries).
- Section 4: Performance Optimization Notes for Large Tables (bulleted, max 4 items).
Self-review
- Verify that the MERGE statement key covers both the employee ID and {{aggregation_granularity}} timestamp.
- Check that overtime flag logic triggers strictly above {{overtime_threshold_hours}}.
- Confirm window functions partition by employee and session boundaries without cross-worker data leakage.
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.