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

  • 相关阅读:
    IntelliJ IDEA常用的快捷键积累总结
    Linux命令(六)之防火墙iptables的相关操作以及端口的开放
    Linux命令(五)之service服务查找、启动/停止等相关操作
    Linux命令(四)之常用文件拷贝/移动,文件解压缩,文件查找等相关的操作
    Linux命令(三)vim编辑器的常用命令
    Linux命令(一)之目录结构、Linux终端操作、关机重启等一些基本操作
    zookeeper核心知识与投票机制详解
    zuul开发实战(限流,超时解决)
    IO多路复用技术详解
    Linux五大网络IO模型图解
  • 原文地址:https://www.cnblogs.com/leeeee/p/11902030.html
Copyright © 2011-2022 走看看