【发布时间】:2020-11-04 09:27:20
【问题描述】:
此表是一个“心跳”跟踪事件,在该流媒体直播期间,每个主播每分钟生成一行。如果主播直播 60 分钟,则此表将生成 60 行
Create Table minute_streamed
(
time_minute datetime ,
username varchar(50) ,
category varchar(50) ,
concurrent_viewers int
);
Insert into minute_streamed values ('2020-03-18 12:00:00', 'alex','Fornite',125) ;
Insert into minute_streamed values ('2020-03-18 12:01:00', 'alex','Fornite',130) ;
Insert into minute_streamed values ('2020-03-19 15:30:00', 'jamie','Just Chatting',13) ;
Insert into minute_streamed values ('2020-03-19 15:31:00', 'jamie','Food & Drink',15) ;
Insert into minute_streamed values ('2020-03-20 10:30:00', 'rick','Call of Duty: Black Ops',150) ;
Insert into minute_streamed values ('2020-03-20 10:31:00', 'rick','Call of Duty: Modern Warfare',120) ;
Insert into minute_streamed values ('2020-04-21 09:30:00', 'rick','Fornite',120) ;
Insert into minute_streamed values ('2020-04-20 10:31:00', 'rick','Call of Duty: Modern Warfare',120) ;
Insert into minute_streamed values ('2020-04-21 09:30:00', 'rick','Fornite',120) ;
Insert into minute_streamed values ('2020-04-20 10:31:00', 'jamie','Call of Duty: Modern Warfare',120) ;
Insert into minute_streamed values ('2020-04-21 09:30:00', 'jamie','Fornite',120) ;
Insert into minute_streamed values ('2020-04-18 12:00:00', 'alex','Fornite',125) ;
Insert into minute_streamed values ('2020-04-18 12:01:00', 'alex','Fornite',130) ;
Insert into minute_streamed values ('2020-06-18 14:00:00', 'alex','Fornite',120) ;
Alex 在三月份有两个条目。这意味着他直播了 2 分钟。因此,他 3 月份的每小时流媒体播放量将是 2/60。 2 分钟/1 小时(60 分钟)
我正在尝试编写一个查询,该查询为每个流媒体返回一行,其中列表示他们的流式传输总小时数(在任何类别中)以及在使命召唤游戏类别中流式传输的小时百分比。
你能帮我修改我的查询吗?
select
username
,case
when if_call_of_duty = 1 then sum(hour_streamed)
end as call_of_hour
from(
select
username
,category
,count(*)/60 as hour_streamed
,CASE
when category like 'Call of Duty%' THEN 1
else 0
end as if_call_of_duty
from minute_streamed
group by username,category) as temp
group by username,if_call_of_duty
我试图得到这样的输出:
username total_hours_streamed percantege_call_of_duty
alex 10 0
rick 10 0.50
每个用户的总时长,以及使命召唤游戏类别的总时长百分比。
【问题讨论】:
标签: mysql sql date count pivot