首页 > 试题广场 >

牛客直播开始时各直播间在线人数

[编程题]牛客直播开始时各直播间在线人数
  • 热度指数:31978 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
牛客某页面推出了数据分析系列直播课程介绍。用户可以选择报名任意一场或多场直播课。
已知课程表course_tb如下(其中course_id代表课程编号,course_name表示课程名称,course_datetime代表上课时间):
course_id course_name course_datetime
1 Python 2021-12-1 19:00-21:00
2 SQL 2021-12-2 19:00-21:00
3 R 2021-12-3 19:00-21:00
上课情况表attend_tb如下(其中user_id表示用户编号、course_id代表课程编号、in_datetime表示进入直播间的时间、out_datetime表示离开直播间的时间):
user_id course_id in_datetime out_datetime
100 1 2021-12-01 19:00:00
2021-12-01 19:28:00
100 1 2021-12-01 19:30:00
2021-12-01 19:53:00
101 1 2021-12-01 19:00:00
2021-12-01 20:55:00
102 1 2021-12-01 19:00:00
2021-12-01 19:05:00
104 1 2021-12-01 19:00:00
2021-12-01 20:59:00
101 2 2021-12-02 19:05:00
2021-12-02 20:58:00
102 2 2021-12-02 18:55:00
2021-12-02 21:00:00
104 2 2021-12-02 18:57:00
2021-12-02 20:56:00
107 2 2021-12-02 19:10:00
2021-12-02 19:18:00
100 3 2021-12-03 19:01:00
2021-12-03 21:00:00
102 3 2021-12-03 18:58:00
2021-12-03 19:05:00
108 3 2021-12-03 19:01:00
2021-12-03 19:56:00
请你统计直播开始时(19:00),各科目的在线人数,以上例子的输出结果为(按照course_id升序排序):
course_id course_name online_num
1 Python 4
2
SQL 2
3 R 1
示例1

输入

CREATE TABLE course_tb(
course_id int(10) NOT NULL, 
course_name char(10) NOT NULL,
course_datetime char(30) NOT NULL);
INSERT INTO course_tb VALUES(1, 'Python', '2021-12-1 19:00-21:00');
INSERT INTO course_tb VALUES(2, 'SQL', '2021-12-2 19:00-21:00');
INSERT INTO course_tb VALUES(3, 'R', '2021-12-3 19:00-21:00');

CREATE TABLE attend_tb(
user_id int(10) NOT NULL, 
course_id int(10) NOT NULL,
in_datetime datetime NOT NULL,
out_datetime datetime NOT NULL
);
INSERT INTO attend_tb VALUES(100, 1, '2021-12-1 19:00:00', '2021-12-1 19:28:00');
INSERT INTO attend_tb VALUES(100, 1, '2021-12-1 19:30:00', '2021-12-1 19:53:00');
INSERT INTO attend_tb VALUES(101, 1, '2021-12-1 19:00:00', '2021-12-1 20:55:00');
INSERT INTO attend_tb VALUES(102, 1, '2021-12-1 19:00:00', '2021-12-1 19:05:00');
INSERT INTO attend_tb VALUES(104, 1, '2021-12-1 19:00:00', '2021-12-1 20:59:00');
INSERT INTO attend_tb VALUES(101, 2, '2021-12-2 19:05:00', '2021-12-2 20:58:00');
INSERT INTO attend_tb VALUES(102, 2, '2021-12-2 18:55:00', '2021-12-2 21:00:00');
INSERT INTO attend_tb VALUES(104, 2, '2021-12-2 18:57:00', '2021-12-2 20:56:00');
INSERT INTO attend_tb VALUES(107, 2, '2021-12-2 19:10:00', '2021-12-2 19:18:00');
INSERT INTO attend_tb VALUES(100, 3, '2021-12-3 19:01:00', '2021-12-3 21:00:00');
INSERT INTO attend_tb VALUES(102, 3, '2021-12-3 18:58:00', '2021-12-3 19:05:00');
INSERT INTO attend_tb VALUES(108, 3, '2021-12-3 19:01:00', '2021-12-3 19:56:00');

输出

1|Python|4
2|SQL|2
3|R|1
with t1 as (
select 
x.course_id
,x.course_name
,in_datetime 
,out_datetime
from course_tb x 
join attend_tb y
using(course_id)
),t2 as (
select course_id,course_name,in_datetime as dt,1 as tag
from t1 
union all
select course_id,course_name,out_datetime as dt,-1 as tag
from t1 
),t3 as (
select course_id,course_name,dt,sum(tag) over(partition by course_id,course_name order by dt) as cnt
from t2
where time(dt)<="19:00:00"
)
select distinct course_id,course_name,last_value(cnt) over(partition by course_id,course_name order by dt rows between unbounded preceding and unbounded following) as online_num
from t3
order by 1

发表于 2025-10-14 11:23:20 回复(0)
 select
        t2.course_id,
        t2.course_name,
        sum(if(time(in_datetime)<='19:00:00',1,0)) as online_num -- 此处使用DATE_FORMAT(in_datetime, '%H:%i:%s')一样效果
    from attend_tb t1 join course_tb t2 using (course_id)
    group by t2.course_id,t2.course_name;
发表于 2025-09-29 21:14:33 回复(0)
select
    at.course_id 
    , course_name
    , count(
        case 
            when in_datetime <= left(course_datetime, 15) and out_datetime >= left(course_datetime, 15) then user_id
            end
        ) as online_num
from attend_tb at
    left join course_tb co on at.course_id = co.course_id
group by at.course_id 
    , course_name
order by course_id
;

发表于 2025-09-25 10:17:33 回复(0)
SELECT course_id, course_name, COUNT(*) AS online_num
FROM course_tb JOIN attend_tb USING(course_id)
WHERE in_datetime <= course_datetime AND out_datetime >= course_datetime
GROUP BY course_id, course_name
ORDER BY course_id;
发表于 2025-04-10 11:06:55 回复(0)
select c.course_id,course_name,count(distinct user_id)
from course_tb c
join attend_tb a on c.course_id=a.course_id
where in_datetime<=course_datetime and out_datetime>course_datetime
group by c.course_id,course_name
order by c.course_id
简易做法,不用管19:00:00
发表于 2025-03-23 15:10:03 回复(0)
select
a.course_id,course_name,count(distinct user_id) online_num
from attend_tb a
inner join course_tb using(course_id)
where date_format(in_datetime ,'%H:%i') <= '19:00'
and   date_format(out_datetime,'%H:%i') >'19:00'
group by a.course_id,course_name

发表于 2024-10-23 11:34:03 回复(0)
select a.course_id,course_name,sum(if(date_format(in_datetime,'%H:%i:%s')<= '19:00:00' and date_format(out_datetime,'%H:%i:%s') > '19:00:00',1,0 ))
from course_tb a right join attend_tb b
on a.course_id = b.course_id
group by a.course_id,course_name
发表于 2024-09-29 22:50:21 回复(0)
select
    course_id,
    course_name,
    sum(course_datetime between in_datetime and out_datetime) online_num
from
    course_tb
    join attend_tb using(course_id)
group by
    course_id,
    course_name
order by
    course_id
发表于 2024-08-01 11:47:02 回复(0)
select course_id,course_name
,sum(uv) online_num
from (--
select a.user_id,b.course_id,b.course_name
,date_format(a.in_datetime,"%H:%i:%s") time_ ,1 uv
from attend_tb a
left join course_tb b on b.course_id=a.course_id
union all
select a.user_id,b.course_id,b.course_name
,date_format(a.out_datetime,"%H:%i:%s") time_ ,-1 uv
from attend_tb a
left join course_tb b on b.course_id=a.course_id
) t
where time_<="19:00:00"
group by course_id,course_name
order by course_id
;

发表于 2024-07-11 15:30:31 回复(0)
select
     t1.course_id
    ,t2.course_name
    ,t1.online_num
from (
    select
         course_id
        ,count(distinct user_id) as online_num
    from attend_tb
    where '19:00:00' between time(in_datetime) and time(out_datetime)
    group by 
        course_id
) as t1
left join course_tb as t2
    on t1.course_id = t2.course_id
order by
    t1.course_id
发表于 2024-05-25 22:34:49 回复(0)
WITH A AS(
    SELECT course_tb.course_id, course_name, in_datetime
    FROM attend_tb
    LEFT JOIN course_tb ON
    attend_tb.course_id = course_tb.course_id
    WHERE DATE_FORMAT(in_datetime,'%H:%i:%s')<='19:00:00'
)
SELECT course_id, course_name, COUNT(*)
FROM A
GROUP BY course_name, course_id
ORDER BY course_id ASC

发表于 2024-04-29 13:55:57 回复(0)
select
    course_id,
    course_name,
    count(case when substring(in_datetime,12,5) 
            <= substring(course_datetime,11,5) then user_id else null end) online_num
from(
    select
        c.course_id,
        c.course_name,
        c.course_datetime,
        a.user_id,
        a.in_datetime,
        a.out_datetime
    from
        course_tb c left join attend_tb a
        on c.course_id = a.course_id
)t
group by
    course_id,
    course_name
order by
    course_id

编辑于 2024-04-19 10:15:17 回复(0)
select ctb.course_id, ctb.course_name
,sum(case when left(ctb.course_datetime, 15) between atb.in_datetime and atb.out_datetime then 1 else 0 end) as online_num
from course_tb ctb join attend_tb atb on ctb.course_id = atb.course_id
group by ctb.course_id, ctb.course_name
order by  ctb.course_id asc
编辑于 2024-01-23 15:35:23 回复(0)
我觉得题不够严谨,从时间来算,在这个期间19-21的期间都算,如果不在,则不算。
with tiaojian as (
select
course_id,
count(distinct case when date_format(in_datetime,"%H:%i:%s")<="19:00:00" and date_format(out_datetime,"%H:%i:%s") >="19:00:00" then user_id end)  as pt
from attend_tb
group by course_id
)


select 
t.course_id,
cb.course_name,
t.pt
from tiaojian t left join course_tb cb 
on t.course_id=cb.course_id
order by t.course_id

编辑于 2023-12-27 15:57:56 回复(0)
select course_id,course_name,
count(distinct user_id) online_num
from course_tb join attend_tb using(course_id)
where substring_index(in_datetime,' ',-1) <= '19:00:00'
and substring_index(out_datetime,' ',-1) >= '19:00:00'
group by course_id,course_name
order by course_id
发表于 2023-11-04 19:11:19 回复(0)
#online要求in_time<19:00,out_time>19:00
#第一步:先把start_time按要求做出来
#第二步:取in_time<start_time, out_time>start_time的记录
with course_time as(
    select course_id, course_name, concat(substr(course_datetime,1,15),':00') as 'start_time'
    from course_tb
)
select ct.course_id, course_name, count(user_id) 'online_num '
from attend_tb at left join course_time ct
on at.course_id=ct.course_id
where in_datetime<=start_time and out_datetime>start_time
group by 1,2
order by 1 asc


发表于 2023-09-05 15:23:32 回复(0)
# 请你统计直播开始时(19:00),各科目的在线人数,以上例子的输出结果为(按照course_id升序排序):
-- 注意是时间获取方式:time(),date_format(),substr(date,12,8),这里不能用hour()函数
select 
  t1.course_id, 
  t1.course_name,
  count(distinct t2.user_id) as online_num
from course_tb t1 
left join attend_tb t2
on t1.course_id= t2.course_id
# where time(in_datetime)<='19:00:00' and time(out_datetime)>='19:00:00'
# where date_format(t2.in_datetime,'%H:%i:%s')<= '19:00:00' and date_format(t2.out_datetime,'%H:%i:%s') >='19:00:00'
where substr(t2.in_datetime,12,8)<= '19:00:00' and substr(t2.out_datetime,12,8) >='19:00:00'
group by t1.course_id, t1.course_name
order by 1;


# select 
#  course_id,in_datetime,
#   date_format(t2.in_datetime,'%H:%i:%s'),  -- 18:55:00
#   substr(t2.in_datetime,12,8), -- 18:55:00
#   if(hour(t2.in_datetime)<19 ,1,0),
#   if(hour(t2.in_datetime)<'19',1,0),
#   if(time(in_datetime)<='19:00:00',1,0)
# from attend_tb t2

发表于 2023-08-11 15:23:29 回复(0)
为什么我的代码将sum改为count就不对呢?sum/count在这里有什么不同吗?不应该都是计算满足括号里条件的记录个数吗?
select course_id, course_name, sum(TIME(in_datetime) <= '19:00:00') as online_num
from (
    select a.course_id, b.course_name, a.user_id, a.in_datetime, a.out_datetime
    from attend_tb as a
    left join course_tb as b
    on a.course_id = b.course_id
) as c
group by course_id, course_name
order by course_id;

发表于 2023-08-01 16:24:37 回复(4)
select
c.course_id
, course_name
, count(c.course_id) online_num
from
attend_tb a left join course_tb c
on a.course_id = c.course_id
where right(in_datetime, 8) <= '19:00:00'
group by a.course_id, course_name
order by course_id
发表于 2023-07-20 09:48:23 回复(0)