Given a table of user receipt scans, write a query to find the top three brand partners by total spend for each user segment over the last quarter at Fetch.
Assume the last quarter is the completed calendar quarter relative to the current date. Exclude scans that cannot be associated with a user segment or brand partner.
Output
- One row per qualifying segment and brand partner, with columns
segment, brand_name, total_spend, and segment_rank.
- Include no more than three brands per segment, ordered by segment, rank, total spend descending, and brand name ascending.