AML SQL Window Functions for Transaction Monitoring
Knowledge Compliance {'label': 'tutorial with code', 'icon': '💻', 'color': '#14b8a6', 'bg_color': '#14b8a6', 'description': 'step-by-step tutorials with executable code examples and implementations.', 'slug': 'tutorial-code'}

SQL Window Functions for Transaction Monitoring

Key Insights

  • Velocity checks, rolling sums, and first/last detection with window functions on a transaction table.
Difficulty: Intermediate Type: Knowledge

Velocity: the core AML query

SELECT account_id, COUNT(*) AS tx_count, SUM(amount) AS total
FROM transactions
WHERE ts >= NOW() - INTERVAL '1 day'
GROUP BY account_id
HAVING tx_count > 10;

Structuring detection needs rolling windows — window functions handle every time range in one scan. The fixed-window version above is the first cut, but structuring is definitionally boundary-avoidant: a criminal moving money just under a daily threshold will split transfers across the midnight boundary, defeating a daily GROUP BY. That is why the rolling version below is the production query.

Rolling sum via windows

SELECT *,
 SUM(amount) OVER (
 PARTITION BY account_id ORDER BY ts
 ROWS BETWEEN 29 PRECEDING AND CURRENT ROW
 ) AS rolling_30d
FROM transactions;

ROWS BETWEEN N PRECEDING AND CURRENT ROW counts exactly N transactions, not N days — for a true 30-day window use RANGE BETWEEN INTERVAL '29 days' PRECEDING AND CURRENT ROW, which includes every transaction in the window regardless of count. The distinction is the most common window-function bug in AML queries, and it changes alert behaviour materially for high-frequency accounts.

First transaction from new device

WITH ranked AS (
 SELECT *, ROW_NUMBER() OVER (
 PARTITION BY account_id, device_id ORDER BY ts
 ) AS rn
 FROM transactions
)
SELECT * FROM ranked WHERE rn = 1;

First-use-of-device patterns feed anomaly detectors and are cheap to compute partition-wise. Combine with peer-group baselines: a device ratio far above the account cohort's median is a stronger signal than the raw first-use event. Note the partition key — account_id, device_id — so rn=1 means "first transaction on this device", not "first transaction ever".

Performance notes

Window functions partition in memory: index (account_id, ts) and keep partitions small. On very large tables, pre-filter by ts before the window, or compute per-account aggregates incrementally with an event-stream engine. Peer-group baselines should come from a precomputed cohort table — joining to a live self-aggregate per alert is a classic query-budget killer.

References

Article Metadata

Bloom Taxonomy Questions

Remember

Which window frame computes a rolling 30-day sum in SQL?

Understand

Why are window functions preferable to self-joins for velocity checks on large transaction tables?

Apply

Write a query that flags accounts whose one-hour transaction count exceeds 10 using a rolling window, without scanning the table more than once.

Further Reading

Feynman Concept Cards

Master each concept: read the ELI5, explore analogies, work examples, and teach it back.

Transaction Monitoring is a concept in transaction monitoring. In simple terms, Transaction Monitoring covers transaction monitoring within Compliance. This compliance concept addresses key topics in the transaction monitoring within compliance domain. Also known as: TM, transact

Analogy
Think of Transaction Monitoring like a security camera watching a bank vault 24/7 — it helps you handle transaction monitoring tasks more effectively.
Example
Consider a scenario where Transaction Monitoring applies: Transaction Monitoring covers transaction monitoring within Compliance. This compliance concept addresses key topics in the transaction monitoring within compliance domain. Also known as: TM, transact...
Find Gaps
What are the key components or steps involved in Transaction Monitoring?
Can you explain Transaction Monitoring without using jargon?
What happens if Transaction Monitoring is not applied correctly?
How does Transaction Monitoring relate to other concepts in transaction monitoring?
Teach Back

Explain Transaction Monitoring as if teaching a colleague who is new to transaction monitoring. Cover: what it is, how it works, and why it matters.

Create

Create a code that demonstrates Transaction Monitoring in a real-world transaction monitoring scenario. Walk through your design decisions.

Show solution
A code for Transaction Monitoring should include: 1. The core components of transaction monitoring 2. How they interact 3. Expected outcomes or outputs
Difficulty: Advanced — 5/5

Related Research

Related Lessons

Stay Updated

Get the latest research summaries delivered to your inbox.