当前位置: 首页 > article >正文

力扣SQL仅数据库(196~569)

196. 删除重复的电子邮箱

题目:编写解决方案 删除 所有重复的电子邮件,只保留一个具有最小 id 的唯一电子邮件。

(对于 SQL 用户,请注意你应该编写一个 DELETE 语句而不是 SELECT 语句。)

(对于 Pandas 用户,请注意你应该直接修改 Person 表。)

数据准备:

Create table If Not Exists Person (Id int, Email varchar(255))
Truncate table Person
insert into Person (id, email) values ('1', 'john@example.com')
insert into Person (id, email) values ('2', 'bob@example.com')
insert into Person (id, email) values ('3', 'john@example.com')

代码实现:

delete from Person where id not in (
    select a.id from (select min(id) id from Person group by email)a);

197. 上升的温度

题目:编写解决方案,找出与之前(昨天的)日期相比温度更高的所有日期的 id 。

数据准备:

Create table If Not Exists Weather (id int, recordDate date, temperature int)
Truncate table Weather
insert into Weather (id, recordDate, temperature) values ('1', '2015-01-01', '10')
insert into Weather (id, recordDate, temperature) values ('2', '2015-01-02', '25')
insert into Weather (id, recordDate, temperature) values ('3', '2015-01-03', '20')
insert into Weather (id, recordDate, temperature) values ('4', '2015-01-04', '30')

思路:

weather1
date_add得出前一天的日期
id recordDate temperatureyesterday
12015/1/1102014/12/31
22015/1/2252015/1/1
32015/1/3202015/1/2
42015/1/4302015/1/3
weather1与weather表join连接
id recordDate temperatureyesterdayyesterday日期的temperature今日的temperature是否大于昨日的temoerature
12015/1/1102014/12/31
22015/1/2252015/1/1101
32015/1/3202015/1/2250
42015/1/4302015/1/3201

代码实现:

with t1 as (select id,temperature,recordDate,date_sub(recordDate,interval 1 day) yesterday from weather)
select t1.id from t1 join weather on Weather.recordDate=t1.yesterday 
where t1.temperature>Weather.temperature;

262. 行程和用户

题目:编写解决方案找出 "2013-10-01" 至 "2013-10-03" 期间非禁止用户(乘客和司机都必须未被禁止)的取消率。非禁止用户即 banned 为 No 的用户,禁止用户即 banned 为 Yes 的用户。其中取消率 Cancellation Rate 需要四舍五入保留 两位小数 

取消率 的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)。

数据准备:

Create table If Not Exists Trips (id int, client_id int, driver_id int, city_id int, status ENUM('completed', 'cancelled_by_driver', 'cancelled_by_client'), request_at varchar(50))
Create table If Not Exists Users (users_id int, banned varchar(50), role ENUM('client', 'driver', 'partner'))
Truncate table Trips
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('1', '1', '10', '1', 'completed', '2013-10-01')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('2', '2', '11', '1', 'cancelled_by_driver', '2013-10-01')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('3', '3', '12', '6', 'completed', '2013-10-01')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('4', '4', '13', '6', 'cancelled_by_client', '2013-10-01')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('5', '1', '10', '1', 'completed', '2013-10-02')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('6', '2', '11', '6', 'completed', '2013-10-02')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('7', '3', '12', '6', 'completed', '2013-10-02')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('8', '2', '12', '12', 'completed', '2013-10-03')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('9', '3', '10', '12', 'completed', '2013-10-03')
insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('10', '4', '13', '12', 'cancelled_by_driver', '2013-10-03')
Truncate table Users
insert into Users (users_id, banned, role) values ('1', 'No', 'client')
insert into Users (users_id, banned, role) values ('2', 'Yes', 'client')
insert into Users (users_id, banned, role) values ('3', 'No', 'client')
insert into Users (users_id, banned, role) values ('4', 'No', 'client')
insert into Users (users_id, banned, role) values ('10', 'No', 'driver')
insert into Users (users_id, banned, role) values ('11', 'No', 'driver')
insert into Users (users_id, banned, role) values ('12', 'No', 'driver')
insert into Users (users_id, banned, role) values ('13', 'No', 'driver')

代码实现:

with t as (select * from Trips where 
    client_id in (select users_id from Users where banned='no')
    and driver_id in (select users_id from Users where banned='no'))
select request_at day,round(sum(if(status != 'completed',1,0))/count(id),2) 'Cancellation Rate' from t
where request_at between '2013-10-01' and '2013-10-03' group by request_at;

511. 游戏玩法1

问题:查询每位玩家 第一次登录平台的日期

数据准备:

Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')

代码实现:

select player_id,min(event_date) from activity group by player_id;

512. 游戏玩法2

问题·:请编写解决方案,描述每一个玩家首次登陆的设备名称

数据准备:

Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity;
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')

代码实现:

with t1 as (select player_id player_id1,min(event_date) event_date1 from activity group by player_id)
select player_id,device_id from activity join t1 on Activity.player_id=t1.player_id1
where event_date=t1.event_date1;

513. 游戏玩法3

问题:编写一个解决方案,同时报告每组玩家和日期,以及玩家到 目前为止 玩了多少游戏。 也就是说,玩家在该日期之前所玩的游戏总数。详细情况请查看示例。

数据准备:

Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5');

代码实现:

select player_id,
sum(games_played)over(partition by player_id order by event_date rows between unbounded preceding and current row) con
from activity;

514. 游戏玩法4

问题:报告在首次登录的第二天再次登录的玩家的 比率,四舍五入到小数点后两位。换句话说,你需要计算从首次登录日期开始至少连续两天登录的玩家的数量,然后除以玩家总数。

数据准备:

Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5');

思路:

筛选出各玩家第一次登录的记录
player_id device_id event_date games_playedplayer_id device_id event_date games_played第二天
122016/3/15122016/3/152016/3/2
122016/3/26232017/6/2512017/6/26
232017/6/251312016/3/202016/3/3
312016/3/20
342018/7/35
两张表内连接,player_id相等且第一次登录的第二天的时间等于原表中该用户的登录时间
player_id device_id event_date games_played第二天player_id device_id event_date games_played
122016/3/152016/3/2122016/3/26

代码实现:

t1 筛选出每个玩家第一次登陆的记录

t2 在该记录的后面添加一列第二天列

t3 将表连接,筛选出玩家有第二天登录的记录

最后求出比值

with t1 as (select player_id player_id1,min(event_date) event_date1 from activity group by player_id)
,t2 as (select *,date_add(event_date1,interval 1 day) date from t1)
,t3 as (select  player_id1 from t2 join activity
    on t2.player_id1=Activity.player_id and t2.date=Activity.event_date)
select round((select count(distinct player_id1) from t3)/(select count(distinct player_id) from activity),2) as fraction
;

569. 员工薪水中位数

问题:编写解决方案,找出每个公司的工资中位数。

数据准备:

Create table If Not Exists Employee (id int, company varchar(255), salary int)
Truncate table Employee
insert into Employee (id, company, salary) values ('1', 'A', '2341')
insert into Employee (id, company, salary) values ('2', 'A', '341')
insert into Employee (id, company, salary) values ('3', 'A', '15')
insert into Employee (id, company, salary) values ('4', 'A', '15314')
insert into Employee (id, company, salary) values ('5', 'A', '451')
insert into Employee (id, company, salary) values ('6', 'A', '513')
insert into Employee (id, company, salary) values ('7', 'B', '15')
insert into Employee (id, company, salary) values ('8', 'B', '13')
insert into Employee (id, company, salary) values ('9', 'B', '1154')
insert into Employee (id, company, salary) values ('10', 'B', '1345')
insert into Employee (id, company, salary) values ('11', 'B', '1221')
insert into Employee (id, company, salary) values ('12', 'B', '234')
insert into Employee (id, company, salary) values ('13', 'C', '2345')
insert into Employee (id, company, salary) values ('14', 'C', '2645')
insert into Employee (id, company, salary) values ('15', 'C', '2645')
insert into Employee (id, company, salary) values ('16', 'C', '2652')
insert into Employee (id, company, salary) values ('17', 'C', '65');

代码实现:

t1 对所有员工的工资进行连续不并列排序

t2 找出有偶数个员工的公司的员工工资

t3 找出有奇数个员工的公司的员工工资

联合t2和t3

with t1 as (select *,row_number() over (partition by company order by salary)ran from employee)
,t2 as (select company,round(max(ran)/2,0)rou1,round(max(ran)/2+1,0)rou2 from t1 group by company having max(ran)%2=0)
,t3 as (select company,round(max(ran)/2+0.5,0) rou from t1 group by company having max(ran)%2=1)
select t1.id,t1.company,t1.salary from t1 join t2 on t1.company=t2.company where ran in (rou1,rou2)
union all
select t1.id,t1.company,t1.salary from t1 join t3 on t1.company=t3.company where ran = rou;


http://www.kler.cn/a/289884.html

相关文章:

  • 闯关leetcode——3178. Find the Child Who Has the Ball After K Seconds
  • 鸿蒙学习基本概念
  • sol机器人pump机器人如何实现盈利的?什么是Pump 扫链机器人?
  • 开发语言中,堆区和栈区的区别
  • 虚拟机安装Ubuntu 24.04服务器版(命令行版)
  • 俏美韵从心出发,与女性一道为健康生活贡献力量
  • AI图像放大工具,图片放大无所不能
  • vue通过html2canvas+jspdf生成PDF问题全解(水印,分页,截断,多页,黑屏,空白,附源码)
  • Kafka【六】Linux下安装Kafka(Zookeeper)集群
  • 【AI】前向和反向传播的关系
  • 深度学习与电网信号故障诊断:基于卷积神经网络和残差网络的应用
  • 【Grafana】Prometheus结合Grafana打造智能监控可视化平台
  • 15、VSCode自定义Markwown编辑环境
  • Spring Cloud Consul 与 Eureka 对比:如何选择最佳服务发现工具
  • 微信小程序客户端与服务端进行WebSocket通信
  • 文本数据分析-(TF-IDF)(2)
  • 初识redis:学习Java客户端
  • 深度学习实用方法 - 调试策略篇
  • 9 月 7-8 日,Rust China Conf 2024 来啦!
  • TPH-YOLOv5:基于Transformer预测头的改进YOLOv5,用于无人机捕获场景的目标检测
  • 华为云征文|华为云Flexus X实例docker部署srs6并调优,协议使用webrtc与rtmp
  • 每天一个数据分析题(五百一十八)- Skip-Gram模型
  • python自动化操作PDF,拆分pdf合并pdf,提取pdf内容
  • 张江创新券的一些介绍
  • 搜维尔科技:数据手套+机械手遥操作,五指触感灵巧手解决方案!
  • selenium无法定位元素的几种解决方案