The part that tripped me up was defining 'consecutive' cleanly.
Break the problem into stages: first aggregate purchases by customer and ISO week, then identify consecutive week pairs using window functions, and finally rank customers within each pair's first week by total rented_copies. Use a single query with CTEs to keep the logic modular and readable.
Pro tip: Explicitly state your assumptions about ISO week handling (e.g., using EXTRACT(ISOYEAR FROM date) and EXTRACT(WEEK FROM date)) and clarify that 'full weeks' means weeks entirely within 2024. This shows attention to edge cases and prevents ambiguity.
Filter purchases to calendar year 2024 and compute ISO year and week for each purchase_date. Aggregate total rented_copies per customer per ISO week.
Use window functions like LAG to compare each week to the previous week for the same customer, checking if the week numbers are consecutive (handling year boundaries).
For each consecutive-week pair, rank customers by their total rented_copies in the first week using RANK() or DENSE_RANK() to include ties.
Filter to rows where the rank is 1, returning customer_id, the first week's start date (or ISO week), and the total rented_copies for that week.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.