【问题标题】:Find users total hours streamed查找用户流式传输的总小时数
【发布时间】: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


    【解决方案1】:

    您的结果似乎与您的数据不一致。根据您的解释和现有查询,我怀疑您正在寻找条件聚合。在 MySQL 中,您可以这样表述:

    select 
        username,
        count(*)/60 as hour_streamed,
        avg(category like 'Call of Duty%') as if_call_of_duty 
    from minute_streamed
    group by username
    

    这会按username 聚合记录; hour_streamed 是每个用户的记录总数(在所有类别中)除以 60if_call_of_duty 是与以'Call of Duty' 开头的类别相关的记录的比率

    【讨论】:

    • if_call_of_duty 列应具有以“使命召唤”开头的类别的每小时百分比。例如,杰米总共播放了 0.0667 小时。来自使命召唤类别的 0.0667 小时的百分比。希望能解释
    • @Eren:这就是查询的作用。它给出了一个 ratio(一个介于 0 和 1 之间的值)——如果你想要一个百分比,你可以将它乘以 100。
    • 你能帮我解决一下“对于每个日历月,输出从上一个日历月开始增加直播时间的主播列表”吗?
    猜你喜欢
    • 2013-09-24
    • 2020-01-08
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多