zoukankan      html  css  js  c++  java
  • 1127. User Purchase Platform

    Table: Spending

    +-------------+---------+
    | Column Name | Type |
    +-------------+---------+
    | user_id | int |
    | spend_date | date |
    | platform | enum |
    | amount | int |
    +-------------+---------+
    The table logs the spendings history of users that make purchases from an online shopping website which has a desktop and a mobile application.
    (user_id, spend_date, platform) is the primary key of this table.
    The platform column is an ENUM type of ('desktop', 'mobile').
    Write an SQL query to find the total number of users and the total amount spent using mobile only, desktop only and both mobile and desktop together for each date.

    The query result format is in the following example:

    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 |
    +---------+------------+----------+--------+

    Result table:
    +------------+----------+--------------+-------------+
    | 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 |
    +------------+----------+--------------+-------------+
    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.

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/user-purchase-platform
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    开始想了好久,准备分别查只有desktop的,只有mobile的,以及both的,发现查只有desktop的的这个太复杂,
    后面换了个思路,先按照spend_date,user_id把只有desktop的,只有mobile的,以及both的全部查出来,
    然后再把这个结果按照spend_date分组计算金额以及人数

    select spend_date,platform, sum(amount) as total_amount, count(user_id) total_users
    from
    (select spend_date, user_id,
    (case count(distinct platform)
    when 1 then platform
    when 2 then 'both'
    end
    ) as platform, sum(amount) as amount
    from Spending
    group by spend_date, user_id
    ) as temp2
    group by spend_date, platform
    但是这样得出来的只有某些spend_date某些user_id
    的,题目要求是不存在也要放在结果里面,只是结果置0而已。我在网上找到了
    这种写法

    select distinct(spend_date), p.platform
    from Spending,
    (select 'desktop' as platform union
    select 'mobile' as platform union
    select 'both' as platform
    ) as p
    这样枚举出来就把结果的前两列查出来了。
    然后把这两步的结果合并就可以了。
    完整的sql如下:

    select temp1.spend_date, temp1.platform,
    ifnull(temp3.total_amount, 0) total_amount,
    ifnull(temp3.total_users,0) total_users
    from
    (select distinct(spend_date), p.platform
    from Spending,
    (select 'desktop' as platform union
    select 'mobile' as platform union
    select 'both' as platform
    ) as p
    ) as temp1
    left join
    (select spend_date,platform, sum(amount) as total_amount, count(user_id) total_users
    from
    (select spend_date, user_id,
    (case count(distinct platform)
    when 1 then platform
    when 2 then 'both'
    end
    ) as platform, sum(amount) as amount
    from Spending
    group by spend_date, user_id
    ) as temp2
    group by spend_date, platform
    ) as temp3
    on temp1.platform = temp3.platform and temp1.spend_date = temp3.spend_date

    作者:couchpotato613
    链接:https://leetcode-cn.com/problems/user-purchase-platform/solution/zhe-ti-hao-nan-by-couchpotato613/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    友盟—安卓巴士【Android开发原创教程大赛】
    iOS开发视频教程下载/iphone开发视频教程下载
    发一个Android开发的外包项目。欢迎外包团队来骚扰
    安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发的每个领域
    安卓巴士精选Android开发教程
    做了一个系列的Android开发教程列表
    深刻理解C#的传值调用和传引用调用
    《CLR Via C# 第3版》笔记之(四) 类中字段的默认赋值
    《CLR Via C# 第3版》笔记之(七) const和readonly
    《CLR Via C# 第3版》笔记之(十二) 事件
  • 原文地址:https://www.cnblogs.com/leeeee/p/11902030.html
Copyright © 2011-2022 走看看