leetcode-mysql 1142. User activity in the past 30 days II + 176. The second highest salary +596. Classes with more than 5 students +597. Friend application I: Overall pass rate

Source:
Disclaimer: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me

1142. User activity in the past 30 days II

Link: https://leetcode-cn.com/problems/user-activity-for-the-past-30-days-ii

Title description:

Table: Activity

±--------------±--------+
| Column Name | Type |
±--------------±--- -----+
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
±--------------±-------- +
The table has no primary key, it may have duplicate rows.
The activity_type column is one of ENUM ("open_session", "end_session", "scroll_down", "send_message").
The table shows user activity on social media sites.
Please note that each session belongs to exactly one user.

Write a SQL query to find the average number of sessions per user for the 30 days ending July 27, 2019 (inclusive), rounded to two decimal places. We only count valid sessions in which the user performed at least one activity during the session.

The query result format is shown in the following example:

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 and 2 in the past 30 Each has 1 session in the day, and user 3 has 2 sessions, so the average is (1 + 1 + 2) / 3 = 1.33.

answer

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;

Usage of datediff

You can also look directly here

The DATEDIFF() function returns the number of days between two dates.
DATEDIFF(date1,date2) The date1 and date2 parameters are valid date or date/time expressions.

SELECT DATEDIFF(‘2008-12-30’,‘2008-12-29’) AS DiffDate # 1
SELECT DATEDIFF(‘2008-12-29’,‘2008-12-30’) AS DiffDate # -1

176. The second highest salary

Link: https://leetcode-cn.com/problems/second-highest-salary

Title description

Write a SQL query to get the second highest salary (Salary) in the Employee table.

±—±-------+
| Id | Salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±----- --+
For example, the above Employee table, the SQL query should return 200 as the second highest salary. If there is no second highest salary, the query should return null.

±--------------------+
| SecondHighestSalary |
±--------------------+
| 200 |
±--------------------+

answer

select 
    ifnull(
        (select distinct(Salary) from Employee order by Salary desc limit 1,1), 
        null) 
    as SecondHighestSalary

mysql limit returns a piece of data

order by createtime desc //descending order; asc: ascending order
LIMIT 1

596. Classes with more than 5 students

Title description

Link: https://leetcode-cn.com/problems/classes-more-than-5-students
There is a courses table with: student (student) and class (course).

Please list all classes with more than or equal to 5 students.

For example, the table:

±--------±-----------+
| student | class |
±--------±-----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
±--------±-----------+
应该输出:

±--------+
| class |
±--------+
| Math |
±--------+

answer

Subquery

select class from (
select class, count(distinct student)as num from courses group by class
) as template_table 
where num >= 5;

Direct grouping:

select class from courses group by class  
having count(distinct(student))>=5

597. Friend Application I: Overall Pass Rate

Title description

Link: https://leetcode-cn.com/problems/friend-requests-i-overall-acceptance-rate
In social applications like Facebook or Twitter, people often send friend applications and receive other people’s friend applications .

表:FriendRequest

±---------------±--------+
| Column Name | Type |
±---------------±- -------+
| sender_id | int |
| send_to_id | int |
| request_date | date |
±---------------±--------+
this The table has no primary key, and it may contain duplicates.
The table contains the ID of the user who sent the request, the ID of the user who accepted the request, and the date of the request.
Table: RequestAccepted

±---------------±--------+
| Column Name | Type |
±---------------±- -------+
| requester_id | int |
| accepter_id | int |
| accept_date | date |
±---------------±--------+
this The table has no primary key, and it may contain duplicates.
The table contains the ID of the user who sent the request, the ID of the user who accepted the request, and the date the request was passed.

Write a query sentence to find the pass rate of the friend's application, expressed with 2 decimal places. The pass rate is divided by the number of applications accepted by friends divided by the total number of applications.

prompt:

Not all the approved friend requests are in the table friend_request. You only need to count the total number of approved applications (regardless of whether they are in the FriendRequest table), and divide it by the total number of applications to get the pass rate.
A friend request sender may send several friend requests to the recipient. Maybe a friend application will be approved several times. In this case, repeated friend requests are only counted once.
If there is no application from a friend, the pass rate is 0.00.

The query result should look like the following example:

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 table:
±------------+
| accept_rate |
±------------+
| 0.8 |
±------------ +
There are a total of 5 requests and 4 different pass requests, so the pass rate is 0.80

Problem solution (the problem is not understood!!)

## 子表一定要有别名啊!!!!
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 

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/112200264