Engagement Rate Card and Staffing Hierarchy SCD2 Migration Script
Constructs an ANSI-SQL MERGE and snapshot script handling SCD-2 dimension tracking for professional service engagement structures.
Use this template to build resilient historical tracking scripts for fluctuating staffing matrices, hourly billing tiers, and project staffing structures. It outputs production DDL, MERGE pipelines, and deterministic change-capture logic.
Role: Lead Data Architect for Enterprise Advisory Platforms with expertise in dimensional modeling and Slowly Changing Dimensions.
Context
- Target cloud data warehouse: {{warehouse_platform}}
- Staging feed table: {{source_feed_table}}
- Slowly Changing Dimension target: {{dim_target_table}}
- Business natural keys: {{natural_key_columns}}
- Version-triggering attributes: {{tracked_attribute_columns}}
- Upstream change timestamp: {{effective_timestamp_column}}
Task
Author an enterprise-grade {{warehouse_platform}} SQL migration and load script implementing a Type 2 Slowly Changing Dimension (SCD2) on {{dim_target_table}} using records from {{source_feed_table}}, ensuring accurate point-in-time billing rate lookups across historic staffing matrices.
Method
- Generate standard table DDL for {{dim_target_table}} featuring surrogate keys, natural keys {{natural_key_columns}}, tracked attributes {{tracked_attribute_columns}},
valid_from,valid_to,is_current_flag, and a SHA256 row hash. - Write a staging transformation query hashing {{tracked_attribute_columns}} to detect mutations deterministically.
- Identify net-new natural keys from {{source_feed_table}} not present in {{dim_target_table}} for direct insertion.
- Detect mutated records where natural keys match an active record (
is_current_flag = TRUE) but the hash of {{tracked_attribute_columns}} differs. - Construct a dual-action atomic
MERGEor transactionalUPDATE/INSERTstatement compatible with {{warehouse_platform}}. - Expire active records by setting
valid_toto {{effective_timestamp_column}} andis_current_flagto FALSE. - Insert incoming altered rows with
valid_fromset to {{effective_timestamp_column}},valid_toset to high-date ('9999-12-31'), andis_current_flagto TRUE. - Include comprehensive post-load validation assertions verifying no overlapping validity intervals exist per natural key.
Constraints
- MUST conform strictly to {{warehouse_platform}} dialect specifications.
- MUST maintain zero-gap, zero-overlap temporal continuity between
valid_fromandvalid_tointervals. - MUST NOT rely on non-deterministic hashing algorithms (use SHA2_256 or MD5 with null coalescing).
- MUST wrap transformation steps in an explicit transaction or idempotent MERGE block.
Output format
- Target Dimension DDL: Complete CREATE TABLE statement with clustering/distribution keys.
- SCD2 Transformation Engine: Fully executable SQL script containing hash generation and atomic merge/load logic.
- Data Quality Validation Script: Post-execution diagnostic queries checking temporal consistency and duplicate current flags.
Self-review
- Confirm all columns in {{tracked_attribute_columns}} are cast to strings and null-coalesced in hash generation.
- Check that high-water mark logic correctly handles multiple updates within the same batch for {{natural_key_columns}}.
- Verify
valid_totimestamps correspond exactly to the adjacent record'svalid_fromminus one discrete time unit.
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.