Niuke SQL big factory interview real questions Niuke live class analysis 5 sets of code and analysis

6. Analysis of Niuke Live Classes (online education industry)

SQL185 Niuke Live Conversion Rate

select course_id,
course_name,
round(sum(if_sign)/sum(if_vw)*100,2) as sign_rate
from course_tb
join behavior_tb using(course_id)
group by course_id,course_name
order by course_id

SQL186 Number of people online in each live room when Niuke live broadcast starts

select course_id,
course_name,
count(distinct user_id) as online_num
from course_tb
join attend_tb using(course_id)
where time(in_datetime)<='19:00:00'
    and time(out_datetime)>='19:00:00'
group by course_id,course_name
order by course_id

SQL187 Niuke live broadcast average viewing time of each subject

  1. The first time I wrote it like this, it turned out that the time for the SQL course was too small. After reading the question carefully, I found that the viewing time is defined as the difference between the time of leaving the live broadcast room and the time of entering the live broadcast room, and the unit is minutes. The time I defined for him does not exceed the total time of the course, because I think too much. . .
select course_name,
round(avg(
    if(timestampdiff(second,in_datetime,out_datetime)/60>120
    ,120,
    timestampdiff(second,in_datetime,out_datetime)/60)
    ),2) as avg_Len
from course_tb
join attend_tb using(course_id)
group by course_name
order by avg_Len desc
  1. The correct answer is as follows:
select course_name,
round(avg(timestampdiff(second,in_datetime,out_datetime)/60),2) as avg_Len
from course_tb
join attend_tb using(course_id)
group by course_name
order by avg_Len desc

SQL188 Niuke live broadcast attendance rate of each subject

  1. Note: There are two connections between table a and table b, user_id and course_id, and because table b contains table a, right join is used. If right is not added, part of the content in table b will be missing, and the attendance output of the SQL in the answer is wrong.
  2. The penultimate line is b.course_id=c.course_id, not a.course_id=c.course_id. If table a is connected instead of table b, a situation similar to that in prompt 1 will appear. The output result is based on table a, and part of the content in table b will be missing. The attendance rate of the SQL in the answer will also be wrong.
select 
c.course_id,
course_name,
round(
count(distinct if(timestampdiff(second,in_datetime,out_datetime)>=600,a.user_id,null))*100
/count(distinct if(if_sign=1,b.user_id,null))
,2) as attend_rate
from attend_tb as a
right join behavior_tb as b on a.user_id=b.user_id and a.course_id=b.course_id
join course_tb as c on b.course_id=c.course_id
group by c.course_id,course_name
order by c.course_id

SQL189 Niuke Live Online Number of Subjects Simultaneously

  1. This question is similar to SQL163 The maximum number of people who are watching each article at the same time, the calculation method is the same

  2. The first step is to take the user to enter the live broadcast room, and assign uv to 1; take the user to leave the live broadcast room, and assign uv to -1;
    A total of 24 records

  3. In the second step, since the sorting in the window function has an accumulative effect, the window function can be used to calculate the instantaneous number of users in the live broadcast room;
    insert image description here

  4. The third step is to take the instantaneous maximum value of the live room of each subject, and sort by course_id.
    insert image description here

select 
course_id,
course_name,
max(uv_cnt) as max_num
from (
    select
    course_id,
    course_name,
    sum(uv)over(partition by course_id order by dt,uv desc) uv_cnt
    from (
        select 
        course_id,
        user_id,
        in_datetime as dt,
        1 as uv 
        from attend_tb
        union all
		select 
        course_id,
        user_id,
        out_datetime as dt,
        -1 as uv 
        from attend_tb
        ) as a
    join course_tb using(course_id)
    ) as b
group by course_id,course_name
order by course_id

Guess you like

Origin blog.csdn.net/qq118640594X/article/details/127378315