User Purchase Platform - Problem

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 NameType
user_idint
spend_datedate
platformenum
amountint

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

Spending
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
Primary Key: (user_id, spend_date, platform)
Note: Each row represents a unique user purchase on a specific platform and date

Input & Output

Example 1 — Mixed Platform Usage
Input 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:

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.

Example 2 — Single Platform Day
Input Table:
user_id spend_date platform amount
1 2019-07-01 mobile 50
2 2019-07-01 mobile 75
Output:
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
💡 Note:

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_date is a valid date
  • platform is either 'desktop' or 'mobile'
  • amount ≥ 0

Visualization

Tap to expand
User Purchase Platform INPUT Spending Table user_id date platform amount 1 2019-07-01 mobile 100 1 2019-07-01 desktop 100 2 2019-07-01 desktop 100 2 2019-07-02 mobile 100 3 2019-07-01 mobile 100 Platform Categories: Mobile Only Desktop Only Both Platforms Goal: For each date, find: - Total users per category - Total amount per category (mobile/desktop/both) ALGORITHM STEPS 1 Find User Platforms GROUP BY user_id, date to get distinct platforms 2 Classify Users Use CASE WHEN to assign: mobile/desktop/both IF count(platform) = 2 --> 'both' ELSE IF platform = 'mobile' --> 'mobile_only' ELSE --> 'desktop_only' 3 Aggregate Results GROUP BY date, platform_type COUNT users, SUM amounts 4 Handle All Categories Use UNION or CROSS JOIN to show 0 for empty groups WITH user_platforms AS ( SELECT user_id, date, ... -- CTE Pattern FINAL RESULT Output Table date platform users amount 07-01 both 1 200 07-01 desktop 1 100 07-01 mobile 1 100 07-02 mobile 1 100 07-02 desktop 0 0 07-02 both 0 0 Summary for 2019-07-01: Both: 1 user Desktop: 1 Mobile: 1 Total: $400 OK - Query Complete All dates show all 3 platform categories with user counts and amounts Key Insight: Use a CTE (Common Table Expression) to first classify each user's platform usage per date, then aggregate by date and platform category. A CROSS JOIN with platform types ensures all categories appear even with zero users, using COALESCE to show 0 instead of NULL. TutorialsPoint - User Purchase Platform | Optimal Solution
Asked in
Amazon 23 Facebook 18 Google 15
34.5K Views
Medium Frequency
~25 min Avg. Time
892 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