Write a query to identify anomalous transaction patterns that deviate significantly from a user's historical baseline.
For this exercise, use completed transactions before each transaction to calculate the user's historical amount baseline. Consider a transaction anomalous when at least three prior transactions exist and its amount is at least two historical standard deviations from the prior mean.
transaction_id, user_id, transaction_timestamp, amount, historical_count, historical_avg, historical_stddev, and deviation_score.transaction_timestamp ascending and transaction_id ascending.| Column | Type | Description |
|---|---|---|
| user_idPK | INT | Unique Chime user identifier |
| user_name | VARCHAR(100) | User display name |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| user_id | INT | User who initiated the transaction |
| transaction_timestamp | TIMESTAMP | Time the transaction occurred |
| amount | DECIMAL(12,2) | Transaction amount |
| status | VARCHAR(20) | Transaction processing status |
| merchant_category | VARCHAR(50) | Merchant classification |
| channel | VARCHAR(20) | Transaction channel |