leetcode-mysql 1142. 過去30天的使用者活動 II + 176. 第二高的薪水 +596. 超過5名學生的課+597. 好友申請 I:總體通過率
技術標籤:Leetcode打卡sql面試mysql
來源:
宣告:如果我侵犯了任何人的權利,請聯絡我,我會刪除
歡迎高手來噴我
文章目錄
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 | 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