Friday Purchase III Problem
Description
LeetCode Problem 3118.
Table: Purchases
+---------------+------+
| Column Name | Type |
+---------------+------+
| user_id | int |
| purchase_date | date |
| amount_spend | int |
+---------------+------+
(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table.
purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.
Each row contains user_id, purchase_date, and amount_spend.
Table: Users
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| membership | enum |
+-------------+------+
user_id is the primary key for this table.
membership is an ENUM (category) type of ('Standard', 'Premium', 'VIP').
Each row of this table indicates the user_id, membership type.
Write a solution to calculate the total spending by Premium and VIP members on each Friday of every week in November 2023. If there are no purchases on a particular Friday by Premium or VIP members, it should be considered as 0.
Return the result table ordered by week of the month, and membership in ascending order.
The result format is in the following example.
Example:
Input:
Purchases table:
+---------+---------------+--------------+
| 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 table:
+---------+------------+
| 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 |
+---------------+-------------+--------------+
Explanation:
- During the first week of November 2023, a transaction occurred on Friday, 2023-11-03, by a Premium member amounting to $1,126. No transactions were made by VIP members on this day, resulting in a value of 0.
- For the second week of November 2023, there was a transaction on Friday, 2023-11-10, and it was made by a VIP member, amounting to $7,473. Since there were no purchases by Premium members that Friday, the output shows 0 for Premium members.
- Similarly, during the third week of November 2023, no transactions by Premium or VIP members occurred on Friday, 2023-11-17, which shows 0 for both categories in this week.
- In the fourth week of November 2023, transactions occurred on Friday, 2023-11-24, involving one Premium member purchase of $5,117 and VIP member purchases totaling $14,933 ($9,692 from one and $5,241 from another).
Note: The output table is ordered by week_of_month and membership in ascending order.
Solution
Approach: Build every (week, membership) pair, then left join Friday spending
The report must contain a row for each of the four Fridays and each of the two memberships, even when nobody bought anything. Aggregating the purchases alone would drop those empty combinations, so we build the full grid first and attach the numbers afterwards.
weeksis a recursive CTE producing 1 to 4. The Fridays in November 2023 are the 3rd, 10th, 17th and 24th, so the week of the month isCEIL(day / 7).membershipsholds the two wanted values,PremiumandVIP.Standardmembers are excluded here by never appearing in the grid.fridaysjoins purchases to users and keeps only Fridays (DAYOFWEEK(...) = 6, since Sunday is 1).- Cross join the grid, left join
fridayson week and membership, andSUMthe amounts. A pair with no purchase givesNULL, whichIFNULLturns into 0.
In Pandas the grid comes from a MultiIndex.from_product, and reindex(..., fill_value=0) supplies the zeros for the missing pairs.
WITH RECURSIVE weeks AS (
SELECT 1 AS week_of_month
UNION ALL
SELECT week_of_month + 1
FROM weeks
WHERE week_of_month < 4
),
memberships AS (
SELECT 'Premium' AS membership
UNION ALL
SELECT 'VIP'
),
fridays AS (
SELECT CEIL(DAYOFMONTH(p.purchase_date) / 7) AS week_of_month,
u.membership,
p.amount_spend
FROM Purchases AS p
JOIN Users AS u ON u.user_id = p.user_id
WHERE DAYOFWEEK(p.purchase_date) = 6
)
SELECT w.week_of_month,
m.membership,
IFNULL(SUM(f.amount_spend), 0) AS total_amount
FROM weeks AS w
CROSS JOIN memberships AS m
LEFT JOIN fridays AS f
ON f.week_of_month = w.week_of_month
AND f.membership = m.membership
GROUP BY w.week_of_month, m.membership
ORDER BY w.week_of_month, m.membership;