LeetCode MySQL 1127. 用户购买平台 *

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

支出表: Spending

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| spend_date  | date    |
| platform    | enum    | 
| amount      | int     |
+-------------+---------+
这张表记录了用户在一个在线购物网站的支出历史,
该在线购物平台同时拥有桌面端('desktop')和手机端('mobile')的应用程序。
这张表的主键是 (user_id, spend_date, platform)。
平台列 platform 是一种 ENUM ,类型为('desktop', 'mobile')。

写一段 SQL 来查找每天 使用手机端用户、 使用桌面端用户、 同时 使用桌面端和手机端的用户人数和总支出金额。

查询结果格式如下例所示:

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           |
+------------+----------+--------------+-------------+ 2019-07-01, 
用户1 同时 使用桌面端和手机端购买, 
用户2 仅 使用了手机端购买,
而用户3 仅 使用了桌面端购买。在 2019-07-02, 
用户2 仅 使用了手机端购买, 
用户3 仅 使用了桌面端购买,
且没有用户 同时 使用桌面端和手机端购买。

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

2. 解题

  • 先造出表的各种组合
select distinct spend_date, "desktop" platform from Spending
union
select distinct spend_date, "mobile" platform from Spending
union
select distinct spend_date, "both" platform from Spending
{"headers": ["spend_date", "platform"], 
"values": [
["2019-07-01", "desktop"], 
["2019-07-02", "desktop"], 
["2019-07-01", "mobile"], 
["2019-07-02", "mobile"], 
["2019-07-01", "both"], 
["2019-07-02", "both"]]}
  • 计算每天,某类属下的总金额、人数
select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_u # 1 total_u 这么写也对,就1个人
from Spending
group by spend_date, user_id
{"headers": ["spend_date", "plat", "total_am", "total_u"], 
"values": [
["2019-07-01", "both", 200, 1], 
["2019-07-01", "mobile", 100, 1], 
["2019-07-01", "desktop", 100, 1], 
["2019-07-02", "mobile", 100, 1], 
["2019-07-02", "desktop", 100, 1]]}
  • 上面2表连接
select p.spend_date, p.platform, t.total_am, t.total_u
from
(select distinct spend_date, "desktop" platform from Spendingunionselect distinct spend_date, "mobile" platform from Spendingunionselect distinct spend_date, "both" platform from Spending
) p
left join
(select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_ufrom Spendinggroup by spend_date, user_id
) t
on p.platform = t.plat and p.spend_date = t.spend_date
{"headers": ["spend_date", "platform", "total_am", "total_u"],"values": [
["2019-07-01", "desktop", 100, 1], 
["2019-07-02", "desktop", 100, 1], 
["2019-07-01", "mobile", 100, 1], 
["2019-07-02", "mobile", 100, 1], 
["2019-07-01", "both", 200, 1], 
["2019-07-02", "both", null, null]]}
  • 对连接后的表,求和
# Write your MySQL query statement below
selectspend_date, platform,ifnull(sum(total_am),0) total_amount,ifnull(sum(total_u),0) total_users
from
(select p.spend_date, p.platform, t.total_am, t.total_ufrom(select distinct spend_date, "desktop" platform from Spendingunionselect distinct spend_date, "mobile" platform from Spendingunionselect distinct spend_date, "both" platform from Spending) pleft join(select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_ufrom Spendinggroup by spend_date, user_id) ton p.platform = t.plat and p.spend_date = t.spend_date
) temp
group by spend_date, platform

我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474568.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

生活中常见的操作系统

一、 操作系统 1.1 操作系统概念 操作系统(Operating System,简称 OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行。 操…

Example3_1

import java.util.*;public class Example3_1{ public static void main(String args[]){ Scanner readernew Scanner(System.in); System.out.println("输入待移位的int型整数:"); int xreader.nextInt(); System.out.println("输入移位量:"); int n…

oracle事务重要属性,Oracle中的事务(2)--属性和隔离级别

事务的属性1.只读属性(read only)只读事务,只执行查询操作,而不允许执行DML(增、删、改)操作,使用只读事务,可以让用户只取到某个时间点的数据。假如有一个机票代售点,有一个管理员想在一个时间点进行统计总共卖出去的…

edge.js架起node.js和.net互操作桥梁

今天要介绍的是edge.js这个github上刚兴起的开源项目,它可以让node.js和.net之间在in-process下互操作。.net版本在4.5及以上,因为.net4.5带来的Task,asyn,await关键字和node.js的Event模型正好匹配。如果你感兴趣的话&#xff0c…

linux历史追溯

1. Linux 历史早在 linux 出现之前的 20 年(大约在 20 世纪 70 年代),就有一个相当稳定而成熟的操作系统存在了。那就是 Linux 的老大哥”Unix”。那么 Linux 和 Unix 有什么关系呢?Linux 的内核是由 Linus Torvalds 在 1991 年的时候给开发出来的&#…

php获取当前世界,php获取网站alexa世界流量排名代码

gooood个人博客网站phpfunction getAlexaRank ($Domain){$line "";$data "";$URL "http://data.alexa.com/data/?cli10&datsnba&url". $Domain ;$fp fopen ($URL ,"r");if ($fp ){while (!feof ($fp )){$line fgets ($f…

“函数调用的左操作数”的理解

参考下面这个网址中两段话,对《C Primer》中的这句话“函数调用的左操作数”进行理解: http://www.caole.net/diary/lvalue.html “C中引入了引用类型(reference type),引用总是引用到某一对象或者函数上,因此当我们使用引用时,相当于对其引用…

LeetCode MySQL 180. 连续出现的数字(cast)

文章目录1. 题目2. 解题1. 题目 编写一个 SQL 查询,查找所有至少连续出现三次的数字。 --------- | Id | Num | --------- | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | --------- 例如,给定上面的 L…

linux应用领域

Linux 应用领域个人桌面领域此领域是传统 linux 应用最薄弱的环节,传统 linux 由于界面简单、操作复杂、应用软件少的缺点,一直被 windows 所压制,但近些年来随着 ubuntu、fedora 等优秀桌面环境的兴起,同时各大硬件厂商对其支持…

怎么在linux下使用ftp服务器,怎么在Linux下建立安全的FTP服务器?

怎么在Linux下建立安全的FTP服务器?2018-08-31 15:53分享人:老牛学习Linux系统时,你可能会遇到网络服务的问题,这里将介绍linux FTP服务器安全及DHCP服务的实现的解决方法,在这里拿出来和大家分享一下。Linux系统相对于…

Linux基本操作指南

Linux 操作1 Linux 文件和目录Windows 和 Linux 文件系统在 windows 平台下,打开“计算机”,我们看到的是一个个的驱动器盘符:每个驱动器都有自己的根目录结构,这样形成了多个树并列的情形。在 Linux 下,我们是看不到这…

jar打包 jar line too long 异常处理方法

http://hi.baidu.com/hoszone/item/e5165353062e2e828d12eddb 在jar的manifest.mf中加入了所依赖的所有jar路径,结果报错说 IOException("line too long") 然后查看这个地方java.util.jar.Attributes.read方法里,针对每行读入的数据有个byte限…

LeetCode MySQL 178. 分数排名(dense_rank连续排名)

文章目录1. 题目2. 解题1. 题目 编写一个 SQL 查询来实现分数排名。 如果两个分数相同,则两个分数排名(Rank)相同。 请注意,平分后的下一个名次应该是下一个连续的整数值。 换句话说,名次之间不应该有“间隔”。 --…

编辑神器Vi介绍及常用命令

1 vi 简介vi 编辑器是 Linux 和 Unix 上最基本的文本编辑器,工作在字符模式下。由于不需要图形界面,vi 是效率很高的文本编辑器。尽管在 Linux 上也有很多图形界面的编辑器可用,但 vi 在系统和服务器管理中的功能是那些图形编辑器所无法比拟的…

LeetCode MySQL 185. 部门工资前三高的所有员工(dense_rank)

文章目录1. 题目2. 解题1. 题目 Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。 --------------------------------- | Id | Name | Salary | DepartmentId | ---------------…

jquery非常不错的图片切换,多种切换效果

下载: http://files.cnblogs.com/timy/wowslider.rar转载于:https://www.cnblogs.com/timy/archive/2013/04/03/2998381.html

linux 子目录 挂载,linux – NFS4 – 挂载多个子目录

我正在尝试在NFS4服务器上设置2个不同的挂载点,但无法使其按预期工作./etc/exports (on server)/mnt/raid/nas 10.1.0.0/18(rw,fsid0,sync)/mnt/raid/nas/file/perm 10.1.0.0/18(rw,sync,all_squash,no_subtree_check,anonuid501,anongid503)/mnt/raid/nas/mail 10.1.0.0/18(rw…

python历史以及基础知识

1. Python 基础知识1.1 Python 历史1.1.1 Python 起源Python 的作者,Guido von Rossum,荷兰人。1982 年,Guido 从阿姆斯特丹大学获得了数学和计算机硕士学位。然而,尽管他算得上是一位数学家,但他更加享受计算机带来的…

SQLSERVER字符串截取------STUFF

STUFF ( character_expression , start , length ,character_expression ) 参数 character_expression 一个字符数据表达式。character_expression 可以是常量、变量,也可以是字符列或二进制数据列。 start 一个整数值,指定删除和插入的开始位置。如…

LeetCode MySQL 601. 体育馆的人流量(row_number+over+cast)

文章目录1. 题目2. 解题1. 题目 X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)。 请编写一个查询语句,找出人流量的高峰期。高峰期时,至少连续三行记录中的人流量…