Friday Purchase III - Problem

You are given two tables: Purchases and Users.

The Purchases table contains information about user purchases in November 2023, with columns for user_id, purchase_date, and amount_spend.

The Users table contains user membership information with user_id and membership type ('Standard', 'Premium', 'VIP').

Task: Calculate the total spending by Premium and VIP members on each Friday of every week in November 2023.

Requirements:

  • Only include Premium and VIP members (exclude Standard)
  • Only include purchases made on Fridays
  • Group results by week of month and membership type
  • Show 0 for weeks/membership combinations with no purchases
  • Order by week_of_month and membership (ascending)

Table Schema

Purchases
Column Name Type Description
user_id PK int User identifier
purchase_date PK date Date of purchase (November 1-30, 2023)
amount_spend PK int Amount spent on purchase
Primary Key: user_id, purchase_date, amount_spend
Users
Column Name Type Description
user_id PK int User identifier
membership enum Membership type: 'Standard', 'Premium', or 'VIP'
Primary Key: user_id

Input & Output

Example 1 — November 2023 Friday Purchases
Input Tables:
Purchases
user_id purchase_date amount_spend
11 2023-11-03 1126
15 2023-11-10 7473
17 2023-11-17 2414
12 2023-11-24 9692
8 2023-11-24 5117
1 2023-11-24 5241
10 2023-11-22 8266
13 2023-11-21 12000
Users
user_id membership
11 Premium
15 VIP
17 Standard
12 VIP
8 Premium
1 VIP
10 Standard
13 Premium
Output:
week_of_month membership total_amount
1 Premium 1126
1 VIP 0
2 Premium 0
2 VIP 7473
3 Premium 0
3 VIP 0
4 Premium 5117
4 VIP 14933
💡 Note:

The solution filters for Friday purchases by Premium/VIP members across November 2023 weeks:

  • Week 1 (Nov 3): Premium member spent $1,126, no VIP purchases
  • Week 2 (Nov 10): VIP member spent $7,473, no Premium purchases
  • Week 3 (Nov 17): Only Standard member purchase (excluded)
  • Week 4 (Nov 24): Premium: $5,117, VIP: $9,692 + $5,241 = $14,933
Example 2 — No Premium/VIP Friday Purchases
Input Tables:
Purchases
user_id purchase_date amount_spend
17 2023-11-03 1000
10 2023-11-10 2000
Users
user_id membership
17 Standard
10 Standard
Output:
week_of_month membership total_amount
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:

When only Standard members make Friday purchases, all Premium and VIP totals show 0. The cross join approach ensures all 8 week-membership combinations appear in results.

Constraints

  • 1 ≤ user_id ≤ 1000
  • purchase_date is between '2023-11-01' and '2023-11-30'
  • 1 ≤ amount_spend ≤ 100000
  • membership is one of ('Standard', 'Premium', 'VIP')

Visualization

Tap to expand
Friday Purchase III - Solution Flow INPUT DATA Purchases Table user_id | purchase_date | amount 1 | 2023-11-03 | 150.00 2 | 2023-11-10 | 200.00 3 | 2023-11-17 | 75.50 Members Table user_id | membership_type 1 | Premium 2 | VIP 3 | Premium Filter Criteria - Month: November 2023 - Day: Fridays only - Members: Premium, VIP - Show 0 for no purchases ALGORITHM STEPS 1 Generate Week Numbers Find all Fridays in Nov 2023 (Week 1-5, Nov 3,10,17,24) 2 CROSS JOIN Combinations Weeks x (Premium, VIP) Creates all week-member pairs 3 LEFT JOIN Purchases Match Friday purchases Filter: DAYOFWEEK() = 6 4 GROUP BY + SUM Aggregate by week, type COALESCE(SUM(), 0) SELECT week_num, membership, COALESCE(SUM(amount),0) FROM weeks CROSS JOIN types LEFT JOIN purchases... GROUP BY 1,2 ORDER BY 1,2 FINAL RESULT week type total 1 Premium 150.00 1 VIP 0.00 2 Premium 0.00 2 VIP 200.00 3 Premium 75.50 3 VIP 0.00 4 Premium 0.00 4 VIP 0.00 ... (more rows) OK - 10 rows returned Ordered: week, membership Key Insight: CROSS JOIN generates all week-membership combinations ensuring 0 values appear for weeks with no purchases. COALESCE handles NULL sums from LEFT JOIN, converting them to 0. DAYOFWEEK()=6 filters for Fridays. TutorialsPoint - Friday Purchase III | Optimal Solution
Asked in
Amazon 28 Microsoft 22 Meta 18
32.0K Views
Medium Frequency
~18 min Avg. Time
890 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