Friday Purchase III - Problem

You're managing a retail analytics system that needs to track Premium and VIP member spending patterns during Friday shopping events in November 2023.

Given two tables:

  • Purchases: Contains user purchase records with user_id, purchase_date, and amount_spend
  • Users: Contains user membership information with user_id and membership (Standard/Premium/VIP)

Your task is to calculate the total spending by Premium and VIP members on each Friday of every week in November 2023. The challenge is to ensure that:

  • Only Friday purchases are considered
  • Only Premium and VIP members are included (ignore Standard members)
  • If no purchases occurred on a Friday by a membership type, show 0
  • Results are grouped by week of the month and membership type

This type of analysis helps businesses understand high-value customer behavior during peak shopping days and optimize their Friday marketing campaigns.

Input & Output

example_1.sql โ€” Basic Friday Analysis
$ Input: Purchases: [(11,'2023-11-03',1126), (15,'2023-11-10',7473), (17,'2023-11-17',2414), (12,'2023-11-24',9692)] Users: [(11,'Premium'), (15,'VIP'), (17,'Standard'), (12,'VIP')]
โ€บ Output: [(1,'Premium',1126), (1,'VIP',0), (2,'Premium',0), (2,'VIP',7473), (3,'Premium',0), (3,'VIP',0), (4,'Premium',0), (4,'VIP',9692)]
๐Ÿ’ก Note: Nov 3rd (Week 1): Premium member spent $1126, no VIP purchases. Nov 10th (Week 2): VIP member spent $7473, no Premium purchases. Nov 17th (Week 3): Only Standard member purchased (ignored). Nov 24th (Week 4): VIP member spent $9692.
example_2.sql โ€” Multiple Same-Day Purchases
$ Input: Purchases: [(8,'2023-11-24',5117), (1,'2023-11-24',5241), (12,'2023-11-24',9692)] Users: [(8,'Premium'), (1,'VIP'), (12,'VIP')]
โ€บ Output: [(1,'Premium',0), (1,'VIP',0), (2,'Premium',0), (2,'VIP',0), (3,'Premium',0), (3,'VIP',0), (4,'Premium',5117), (4,'VIP',14933)]
๐Ÿ’ก Note: All purchases on Nov 24th (Week 4): Premium member spent $5117, two VIP members spent $5241 + $9692 = $14933 total. All other weeks show 0 for both membership types.
example_3.sql โ€” No Premium/VIP Friday Purchases
$ Input: Purchases: [(17,'2023-11-03',1000), (10,'2023-11-10',2000), (13,'2023-11-17',3000)] Users: [(17,'Standard'), (10,'Standard'), (13,'Standard')]
โ€บ Output: [(1,'Premium',0), (1,'VIP',0), (2,'Premium',0), (2,'VIP',0), (3,'Premium',0), (3,'VIP',0), (4,'Premium',0), (4,'VIP',0)]
๐Ÿ’ก Note: All purchases were made by Standard members, which are filtered out. Result shows 0 for all Premium/VIP combinations across all weeks, demonstrating proper handling of missing data.

Constraints

  • purchase_date ranges from November 1, 2023, to November 30, 2023 inclusive
  • membership is ENUM type with values ('Standard', 'Premium', 'VIP')
  • 1 โ‰ค user_id โ‰ค 1000
  • 1 โ‰ค amount_spend โ‰ค 105
  • Only Friday purchases by Premium/VIP members should be included
  • Results must be ordered by week_of_month and membership in ascending order

Visualization

Tap to expand
November 2023 Friday Purchase AnalysisWeek 1Nov 3 (Fri)Premium: $1,126VIP: $0Week 2Nov 10 (Fri)Premium: $0VIP: $7,473Week 3Nov 17 (Fri)Premium: $0VIP: $0Week 4Nov 24 (Fri)Premium: $5,117VIP: $14,933LegendHas PurchasesNo Purchases (0)Premium/VIP OnlySQL filters Fridays using DAYOFWEEK(date) = 6 and joins with Users table for membership filtering
Understanding the Visualization
1
Identify Target Days
Find all Fridays in November 2023 (3rd, 10th, 17th, 24th)
2
Filter Premium Customers
Focus only on Premium and VIP membership levels
3
Group by Week & Membership
Aggregate spending for each Friday by membership type
4
Fill Missing Data
Show 0 for weeks/memberships with no Friday purchases
Key Takeaway
๐ŸŽฏ Key Insight: Use SQL's date functions and JOINs to efficiently filter, aggregate, and handle missing data in a single query rather than multiple manual operations.
Asked in
Amazon 45 Meta 38 Google 32 Microsoft 28
42.2K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen