You are given a table Spending that logs the history of user purchases from an online shopping website with both desktop and mobile applications.
Table: Spending
| Column Name | Type |
|---|---|
| user_id | int |
| spend_date | date |
| platform | enum |
| amount | int |
The combination (user_id, spend_date, platform) is the primary key of this table. The platform column is an ENUM type with values ('desktop', 'mobile').
Task: Write a solution to find the total number of users and the total amount spent using:
- mobile only - users who spent on mobile platform exclusively on that date
- desktop only - users who spent on desktop platform exclusively on that date
- both - users who spent on both mobile and desktop platforms on that date
Return the result for each date in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | User identifier |
spend_date
PK
|
date | Date of the purchase |
platform
PK
|
enum | Platform used: 'desktop' or 'mobile' |
amount
|
int | Amount spent in the transaction |
Input & Output
| 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 |
| spend_date | platform | total_users | total_amount |
|---|---|---|---|
| 2019-07-01 | both | 1 | 200 |
| 2019-07-01 | desktop | 1 | 100 |
| 2019-07-01 | mobile | 1 | 100 |
| 2019-07-02 | both | 0 | 0 |
| 2019-07-02 | desktop | 1 | 100 |
| 2019-07-02 | mobile | 1 | 100 |
For 2019-07-01: User 1 spent on both platforms (both: 1 user, $200), User 2 spent only on mobile (mobile: 1 user, $100), User 3 spent only on desktop (desktop: 1 user, $100). For 2019-07-02: User 2 spent only on mobile and User 3 spent only on desktop, with no users using both platforms.
| user_id | spend_date | platform | amount |
|---|---|---|---|
| 1 | 2019-07-01 | mobile | 50 |
| 2 | 2019-07-01 | mobile | 75 |
| spend_date | platform | total_users | total_amount |
|---|---|---|---|
| 2019-07-01 | both | 0 | 0 |
| 2019-07-01 | desktop | 0 | 0 |
| 2019-07-01 | mobile | 2 | 125 |
All users only used mobile platform on this date. The cross join ensures that all three platform categories appear in the result, with desktop and both showing zero counts.
Constraints
-
1 ≤ user_id ≤ 1000 -
spend_dateis a valid date -
platformis either'desktop'or'mobile' -
amount ≥ 0