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)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    初学Delphi,如何用delphi编写ini文件设置SQL数据库的连接!急!(100分)
    Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)
    [DELPHI]TreeView精确定位到每一个ITEM
    Oracle查看并修改最大连接数
    004-行为型-03-观察者模式(Observer)
    008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required
    java-mybaits-016-mybatis知识点StatementType
    004-行为型-02-模板方法模式(Template Method)
    004-行为型-01-策略模式(Strategy)
    java-mybaits-015-mybatis逆向工程最佳实践【基础mybatis-generator、tk.mybatis、mubatis-plus】
  • 原文地址:https://www.cnblogs.com/leeeee/p/11902030.html
Copyright © 2011-2022 走看看