来源:
声明:如果我侵犯了任何人的权利,请联系我,我会删除
欢迎高手来喷我
文章目录
1142. 过去30天的用户活动 II
链接:https://leetcode-cn.com/problems/user-activity-for-the-past-30-days-ii
题目描述:
Table: Activity
±--------------±--------+
| Column Name | Type |
±--------------±--------+
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
±--------------±--------+
该表没有主键,它可能有重复的行。
activity_type 列是 ENUM(“ open_session”,“ end_session”,“ scroll_down”,“ send_message”)中的某一类型。
该表显示了社交媒体网站的用户活动。
请注意,每个会话完全属于一个用户。
编写SQL查询以查找截至2019年7月27日(含)的30天内每个用户的平均会话数,四舍五入到小数点后两位。我们只统计那些会话期间用户至少进行一项活动的有效会话。
查询结果格式如下例所示:
Activity table:
±--------±-----------±--------------±--------------+
| user_id | session_id | activity_date | activity_type |
±--------±-----------±--------------±--------------+
| 1 | 1 | 2019-07-20 | open_session |
| 1 | 1 | 2019-07-20 | scroll_down |
| 1 | 1 | 2019-07-20 | end_session |
| 2 | 4 | 2019-07-20 | open_session |
| 2 | 4 | 2019-07-21 | send_message |
| 2 | 4 | 2019-07-21 | end_session |
| 3 | 2 | 2019-07-21 | open_session |
| 3 | 2 | 2019-07-21 | send_message |
| 3 | 2 | 2019-07-21 | end_session |
| 3 | 5 | 2019-07-21 | open_session |
| 3 | 5 | 2019-07-21 | scroll_down |
| 3 | 5 | 2019-07-21 | end_session |
| 4 | 3 | 2019-06-25 | open_session |
| 4 | 3 | 2019-06-25 | end_session |
±--------±-----------±--------------±--------------+
Result table:
±--------------------------+
| average_sessions_per_user |
±--------------------------+
| 1.33 |
±--------------------------+
User 1 和 2 在过去30天内各自进行了1次会话,而用户3进行了2次会话,因此平均值为(1 +1 + 2)/ 3 = 1.33。
题解
select ifnull(round(count(distinct(session_id)) / count(distinct(user_id)),2), 0) as average_sessions_per_user
from Activity
-- where activity_date between "2019-06-28" and "2019-07-27" ;
where datediff("2019-07-27", activity_date) < 30;
datediff的用法
DATEDIFF() 函数返回两个日期之间的天数。
DATEDIFF(date1,date2) date1 和 date2 参数是合法的日期或日期/时间表达式。
SELECT DATEDIFF(‘2008-12-30’,‘2008-12-29’) AS DiffDate # 1
SELECT DATEDIFF(‘2008-12-29’,‘2008-12-30’) AS DiffDate # -1
176. 第二高的薪水
链接:https://leetcode-cn.com/problems/second-highest-salary
题目描述
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
±—±-------+
| Id | Salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±-------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
±--------------------+
| SecondHighestSalary |
±--------------------+
| 200 |
±--------------------+
题解
select
ifnull(
(select distinct(Salary) from Employee order by Salary desc limit 1,1),
null)
as SecondHighestSalary
mysql限定返回一条数据
order by createtime desc //降序;asc:升序
LIMIT 1
596. 超过5名学生的课
题目描述
链接:https://leetcode-cn.com/problems/classes-more-than-5-students
有一个courses 表 ,有: student (学生) 和 class (课程)。
请列出所有超过或等于5名学生的课。
例如,表:
±--------±-----------+
| student | class |
±--------±-----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
±--------±-----------+
应该输出:
±--------+
| class |
±--------+
| Math |
±--------+
题解
子查询
select class from (
select class, count(distinct student)as num from courses group by class
) as template_table
where num >= 5;
直接分组:
select class from courses group by class
having count(distinct(student))>=5
597. 好友申请 I:总体通过率
题目描述
链接:https://leetcode-cn.com/problems/friend-requests-i-overall-acceptance-rate
在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。
表:FriendRequest
±---------------±--------+
| Column Name | Type |
±---------------±--------+
| sender_id | int |
| send_to_id | int |
| request_date | date |
±---------------±--------+
此表没有主键,它可能包含重复项。
该表包含发送请求的用户的 ID ,接受请求的用户的 ID 以及请求的日期。
表:RequestAccepted
±---------------±--------+
| Column Name | Type |
±---------------±--------+
| requester_id | int |
| accepter_id | int |
| accept_date | date |
±---------------±--------+
此表没有主键,它可能包含重复项。
该表包含发送请求的用户的 ID ,接受请求的用户的 ID 以及请求通过的日期。
写一个查询语句,求出好友申请的通过率,用 2 位小数表示。通过率由接受好友申请的数目除以申请总数。
提示:
通过的好友申请不一定都在表 friend_request 中。你只需要统计总的被通过的申请数(不管它们在不在表 FriendRequest 中),并将它除以申请总数,得到通过率
一个好友申请发送者有可能会给接受者发几条好友申请,也有可能一个好友申请会被通过好几次。这种情况下,重复的好友申请只统计一次。
如果一个好友申请都没有,通过率为 0.00 。
查询结果应该如下例所示:
FriendRequest 表:
±----------±-----------±-------------+
| sender_id | send_to_id | request_date |
±----------±-----------±-------------+
| 1 | 2 | 2016/06/01 |
| 1 | 3 | 2016/06/01 |
| 1 | 4 | 2016/06/01 |
| 2 | 3 | 2016/06/02 |
| 3 | 4 | 2016/06/09 |
±----------±-----------±-------------+
RequestAccepted 表:
±-------------±------------±------------+
| requester_id | accepter_id | accept_date |
±-------------±------------±------------+
| 1 | 2 | 2016/06/03 |
| 1 | 3 | 2016/06/08 |
| 2 | 3 | 2016/06/08 |
| 3 | 4 | 2016/06/09 |
| 3 | 4 | 2016/06/10 |
±-------------±------------±------------+
Result 表:
±------------+
| accept_rate |
±------------+
| 0.8 |
±------------+
总共有 5 个请求,有 4 个不同的通过请求,所以通过率是 0.80
题解(题目没有读懂!!)
## 子表一定要有别名啊!!!!
select round(
ifnull(
(select count(*) from (select distinct requester_id, accepter_id from RequestAccepted) as b)
/
(select count(*) from (select distinct sender_id, send_to_id from FriendRequest) as a)
,0)
,2) as accept_rate