This question evaluates proficiency in SQL joins, aggregation, and window functions for ranked group-level analytics, specifically testing the ability to compute partitioned ranks, handle ties, and apply time-based filters in a data engineering context.
You are given two tables:
accountsaccount_id
(string)
customer_id
(string)
segment
(string) — e.g.,
"prime"
,
"subprime"
transactionstransaction_id
(string)
account_id
(string)
amount
(numeric)
txn_ts
(timestamp)
For each segment, find the top 3 customers by total transaction amount in the last 30 days (inclusive).
Requirements:
Return columns:
segment
customer_id
total_amount_30d
rank_in_segment
Order results by segment, then rank_in_segment, then customer_id.