User Purchase Platform Problem
#1127
Description
LeetCode Problem 1127.
Table: Spending
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| spend_date | date |
| platform | enum |
| amount | int |
+-------------+---------+
The table logs the spendings history of users that make purchases from an online shopping website which has a desktop and a mobile application.
(user_id, spend_date, platform) is the primary key of this table.
The platform column is an ENUM type of ('desktop', 'mobile').
Write an SQL query to find the total number of users and the total amount spent using mobile only, desktop only and both mobile and desktop together for each date.
The query result format is in the following example:
Spending table:
+---------+------------+----------+--------+
| user_id | spend_date | platform | amount |
+---------+------------+----------+--------+
| 1 | 2019-07-01 | mobile | 100 |
| 1 | 2019-07-01 | desktop | 100 |
| 2 | 2019-07-01 | mobile | 100 |
| 2 | 2019-07-02 | mobile | 100 |
| 3 | 2019-07-01 | desktop | 100 |
| 3 | 2019-07-02 | desktop | 100 |
+---------+------------+----------+--------+
Result table:
+------------+----------+--------------+-------------+
| spend_date | platform | total_amount | total_users |
+------------+----------+--------------+-------------+
| 2019-07-01 | desktop | 100 | 1 |
| 2019-07-01 | mobile | 100 | 1 |
| 2019-07-01 | both | 200 | 1 |
| 2019-07-02 | desktop | 100 | 1 |
| 2019-07-02 | mobile | 100 | 1 |
| 2019-07-02 | both | 0 | 0 |
+------------+----------+--------------+-------------+
On 2019-07-01, user 1 purchased using both desktop and mobile, user 2 purchased using mobile only and user 3 purchased using desktop only.
On 2019-07-02, user 2 purchased using mobile only, user 3 purchased using desktop only and no one purchased using both platforms.
Solution
Approach: Label Each User-Day, Then LEFT JOIN a Date x Platform Grid
The result has three rows for every date, even when a group is empty (like both on 2019-07-02), so the query has two parts.
- Label each user per day. Group
Spendingbyuser_idandspend_date. If the user used two distinct platforms that day, the label isboth; otherwise it is the one platform they used. Sum theiramountfor the day. A user who bought on both platforms then counts once, underboth, with both amounts added. - Build the grid. Take every distinct
spend_dateand cross join it with the three labelsdesktop,mobileandboth.
LEFT JOIN the labeled user-days onto the grid and group by date and label. COUNT(pu.user_id) gives 0 for an empty group, and COALESCE(SUM(...), 0) turns its NULL sum into 0.
The Pandas version labels each user-day the same way, sums amounts and counts users per date and label, then reindexes on every date with all three labels, filling 0 for missing groups.
WITH per_user AS (
-- One row per user and day, labeled with the platform(s) used that day.
SELECT user_id, spend_date,
IF(COUNT(DISTINCT platform) = 2, 'both', MAX(platform)) AS platform,
SUM(amount) AS amount
FROM Spending
GROUP BY user_id, spend_date
),
date_platforms AS (
-- Every date with all three labels, so empty groups still appear.
SELECT DISTINCT s.spend_date, p.platform
FROM Spending AS s
CROSS JOIN (
SELECT 'desktop' AS platform
UNION ALL SELECT 'mobile'
UNION ALL SELECT 'both'
) AS p
)
SELECT dp.spend_date,
dp.platform,
COALESCE(SUM(pu.amount), 0) AS total_amount,
COUNT(pu.user_id) AS total_users
FROM date_platforms AS dp
LEFT JOIN per_user AS pu
ON pu.spend_date = dp.spend_date
AND pu.platform = dp.platform
GROUP BY dp.spend_date, dp.platform;