Structure the query as a series of CTEs that progressively aggregate data at the country-industry-week grain, then use window functions to compute lagged metrics and week-over-week changes. Join chargebacks to payments via payment_id to correctly attribute late-arriving chargebacks to the original payment's week. Finally, filter for weeks where the chargeback rate increase exceeds 50% and top merchant concentration is below 40%.
Pro tip: Always clarify the definition of 'chargeback rate' (e.g., chargebacks per succeeded payment or per GMV) and confirm the week truncation method (ISO week starting Monday) with the interviewer before writing the query. Explicitly state your assumptions about late-arriving data and how the join through payment_id handles it.
Create a CTE that aggregates payments and chargebacks at the country-industry-week level, using ISO week truncation (date_trunc('week', payment_date)) and joining chargebacks via payment_id to attribute them to the payment's week.
In the base CTE, calculate GMV (sum of payment amounts), active merchants (count distinct merchant_id), succeeded payment counts, and chargeback counts. Then compute the chargeback rate as chargebacks divided by succeeded payments.
Use window functions (LAG) partitioned by country and industry, ordered by week, to get the previous week's chargeback rate. Compute the week-over-week change as (current_rate - lagged_rate) / lagged_rate.
For each country-industry-week, calculate the share of GMV from the top merchant. This can be done with a subquery or window function to rank merchants by GMV and then take the max share.
Filter the results to rows where the WoW chargeback rate increase exceeds 50% (i.e., (current_rate - lagged_rate)/lagged_rate > 0.5) and the top merchant share is below 40%. Ensure the final output includes all required metrics.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.