高级农民
- 积分
- 4176
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
1127. User Purchase Platform
Hard
Topics
SQL Schema
Pandas Schema
Table: Spending
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| spend_date | date |
| platform | enum |
| amount | int |
+-------------+---------+
The table logs the history of the spending of users that make purchases from an online shopping website that has a desktop and a mobile application.
(user_id, spend_date, platform) is the primary key (combination of columns with unique values) of this table.
The platform column is an ENUM (category) type of ('desktop', 'mobile').
Write a solution to find the total number of users and the total amount spent using the mobile only, the desktop only, and both mobile and desktop together for each date.
Return the result table in any order.
The result format is in the following example.
Example 1:
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_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 |
+------------+----------+--------------+-------------+
Explanation:
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.- # Write your MySQL query statement below
- WITH user_daily AS (
- SELECT
- user_id,
- spend_date,
- SUM(amount) AS total_amount,
- COUNT(DISTINCT platform) AS platform_count,
- MAX(platform) AS platform
- FROM Spending
- GROUP BY user_id, spend_date
- ),
- daily_summary AS (
- SELECT
- spend_date,
- CASE
- WHEN platform_count = 2 THEN 'both'
- ELSE platform
- END AS platform,
- SUM(total_amount) AS total_amount,
- COUNT(*) AS total_users
- FROM user_daily
- GROUP BY
- spend_date,
- CASE
- WHEN platform_count = 2 THEN 'both'
- ELSE platform
- END
- ),
- dates AS (
- SELECT DISTINCT spend_date
- FROM Spending
- ),
- platforms AS (
- SELECT 'desktop' AS platform
- UNION ALL
- SELECT 'mobile'
- UNION ALL
- SELECT 'both'
- )
- SELECT
- d.spend_date,
- p.platform,
- COALESCE(s.total_amount, 0) AS total_amount,
- COALESCE(s.total_users, 0) AS total_users
- FROM dates d
- CROSS JOIN platforms p
- LEFT JOIN daily_summary s
- ON d.spend_date = s.spend_date
- AND p.platform = s.platform;
复制代码 |
|