User Purchase Platform - Problem
You're analyzing user spending patterns for a major e-commerce platform that offers both desktop and mobile shopping experiences. Given a table of user purchase history, you need to categorize users based on their platform usage patterns and calculate spending analytics.

For each date, determine:
• Users who shop only on mobile and their total spending
• Users who shop only on desktop and their total spending
• Users who shop on both platforms and their total spending

The Spending table contains:
user_id: Unique identifier for each user
spend_date: Date of purchase
platform: Either 'mobile' or 'desktop'
amount: Money spent in that transaction

Note: A user can make multiple purchases on the same platform on the same day, but each (user_id, spend_date, platform) combination is unique.

Input & Output

example_1.sql — Basic Platform Usage
$ Input: 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 |
Output: | 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 |
💡 Note: On 2019-07-01: User 1 used both platforms (total $200), User 2 used only mobile ($100), User 3 used only desktop ($100). On 2019-07-02: Users 2 and 3 each used only one platform, so 'both' has 0 users.
example_2.sql — Multiple Transactions
$ Input: Spending table: | user_id | spend_date | platform | amount | |---------|------------|----------|--------| | 1 | 2019-07-01 | mobile | 50 | | 1 | 2019-07-01 | mobile | 50 | | 1 | 2019-07-01 | desktop | 100 | | 2 | 2019-07-01 | desktop | 200 |
Output: | spend_date | platform | total_users | total_amount | |------------|----------|-------------|---------------| | 2019-07-01| both | 1 | 200 | | 2019-07-01| desktop | 1 | 200 | | 2019-07-01| mobile | 0 | 0 |
💡 Note: User 1 made multiple mobile transactions ($100 total) plus desktop ($100), so counted as 'both' with $200 total. User 2 only used desktop ($200). No users used mobile only.
example_3.sql — Single Platform Day
$ Input: Spending table: | user_id | spend_date | platform | amount | |---------|------------|----------|--------| | 1 | 2019-07-01 | mobile | 100 | | 2 | 2019-07-01 | mobile | 150 | | 3 | 2019-07-01 | mobile | 200 |
Output: | spend_date | platform | total_users | total_amount | |------------|----------|-------------|---------------| | 2019-07-01| both | 0 | 0 | | 2019-07-01| desktop | 0 | 0 | | 2019-07-01| mobile | 3 | 450 |
💡 Note: All users only used mobile platform on this date. The 'both' and 'desktop' categories show 0 users and $0 amount, demonstrating how the query ensures all platform combinations are represented.

Constraints

  • 1 ≤ number of rows in Spending table ≤ 105
  • 1 ≤ user_id ≤ 109
  • spend_date is a valid date in format YYYY-MM-DD
  • platform is either 'mobile' or 'desktop'
  • 1 ≤ amount ≤ 104
  • Each (user_id, spend_date, platform) combination is unique
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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