返回介绍

solution / 1100-1199 / 1127.User Purchase Platform / README_EN

发布于 2024-06-17 01:03:23 字数 3453 浏览 0 评论 0 收藏 0

1127. User Purchase Platform

中文文档

Description

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.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  P AS (
    SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
    UNION
    SELECT DISTINCT spend_date, 'mobile' FROM Spending
    UNION
    SELECT DISTINCT spend_date, 'both' FROM Spending
  ),
  T AS (
    SELECT
      user_id,
      spend_date,
      SUM(amount) AS amount,
      IF(COUNT(platform) = 1, platform, 'both') AS platform
    FROM Spending
    GROUP BY 1, 2
  )
SELECT
  p.*,
  IFNULL(SUM(amount), 0) AS total_amount,
  COUNT(t.user_id) AS total_users
FROM
  P AS p
  LEFT JOIN T AS t USING (spend_date, platform)
GROUP BY 1, 2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文