【问题标题】:Get comments per video and total sum of comments per video channel获取每个视频的评论和每个视频频道的评论总数
【发布时间】:2019-12-20 15:09:06
【问题描述】:

除了Get total sum of video comments for each video,我注意到我无法使用 v.commentCount,因为我清理了一些数据,现在该列不准确。

所以我需要用评论表统计videoId cmets和总videoID cmets。

我有一个餐桌视频

videoId | channelId
a       | 1        
b       | 1        
c       | 2
d       | 2        

还有一张桌子。 video.channelId 映射到 cmets.videoID_channelID

   commentID | replyID | videoID | videoID_channelID | textOriginal
   c1        | NULL    | a       | 1                 | "test"
   c2        | NULL    | a       | 1                 | "some text"
   NULL      | r1.c1   | b       | 1                 | "asdad"
   NULL      | r2.c1   | b       | 1                 | "xxx"
   c5        | NULL    | b       | 1                 | "yyy"
   c6        | NULL    | c       | 2                 | "zzz"
   c7        | NULL    | d       | 2                 | "-...-."

对于每个 videoId,我需要 commentCount(每个 videoId 有多少 cmets)和该频道所有 commentCounts 的总和(channelID 的每个视频总共有多少 cmets?)。

所以最终结果应该是:

videoId | channelId | commentCount | commentCount_sum
a       | 1         | 2            | 5
b       | 1         | 3            | 5
c       | 2         | 1            | 2
d       | 2         | 1            | 2

到目前为止我的代码:

SELECT v.videoId,
        v.channelId,,,
        count(c.textOriginal) commentCount,
        count(c.textOriginal) over (partition by c.videoID_channelID) as commentCount_sum,
FROM videos v
JOIN comments c
ON v.videoId = c.videoID
GROUP BY v.videoId
ORDER BY commentCount_sum ASC

但我没有得到正确的commentCount_sum?

【问题讨论】:

  • 什么是sentiment_sumtextOriginal?编辑您的问题并解释您想要什么。
  • 已编辑。 text = textOriginal,sentiment_sum 是另一列,我现在计算 c.textOriginal 因为 c.* 是被禁止的。我只需要单个视频的评论计数和每个视频的聚合频道评论计数。

标签: sql sqlite group-by count sum


【解决方案1】:

您可以在子查询中聚合,然后在外部查询中进行窗口计数:

select t.*, sum(commentCount) over(partition by channelId) commentCount_sum
from (
    select v.videoId, v.channelId, count(*) commentCount
    from video v
    inner join comments c on c.videoID = v.videoId and c.videoID_channelID = v.channelId
    group by v.videoId, v.channelId
) t

我还修复了您的加入条件(您在 channelId 上缺少加入条件)。

Demo on DB Fiddle

视频ID |频道 ID |评论数 | commentCount_sum :-------- | :-------- | :----------- | :--------------- 一个 | 1 | 2 | 5 乙 | 1 | 3 | 5 c | 2 | 1 | 2 d | 2 | 1 | 2

【讨论】:

  • 我使用 SQLite 版本 3.30.1 并在第 1 行得到“滥用窗口函数 count()”。
【解决方案2】:

这样就可以了:

SELECT v.videoId,
        v.channelId,
        count(c.textOriginal) commentCount,
        (select count(c.textOriginal) from comments d where d.videoID_channelID = v.channelId group by d.videoID_channelID) as commentCount_sum,
FROM videos v
JOIN comments c
ON v.videoId = c.videoID
GROUP BY v.videoId
ORDER BY commentCount_sum ASC

【讨论】:

    【解决方案3】:

    如果您想在不使用window function 的情况下执行此操作...我将它们隔开,您可以看到我们到底在做什么。正如 GMB 指出的那样,您错过了 channelID 上的连接。

    select a.*, b.channel_comment_count
    from 
    
      (select v.videoid, v.channelid, count(c.textoriginal) as video_comment_count
       from cte1 v join cte2 c on v.videoid = c.videoid and v.channelid = c.videoid_channelid 
       group by v.channelid, v.videoid) a
    
      inner join
    
      (select channelid, count(c.textoriginal) as channel_comment_count
       from cte1 v join cte2 c on v.videoid = c.videoid and v.channelid = c.videoid_channelid 
       group by v.channelid) b
    
      on a.channelid=b.channelid
    

     with cte as
    (select v.*, c.textOriginal from cte1 v join cte2 c on v.videoid = c.videoid and v.channelid = c.videoid_channelid)
    
    select a.*, b.channel_comment_count
    from 
        (select videoid, channelid, count(textoriginal) as video_comment_count
        from cte group by channelid, videoid) a
        inner join
        (select channelid, count(textoriginal) as channel_comment_count
        from cte group by channelid) b
        on a.channelid=b.channelid
    

    DEMO

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 1970-01-01
      • 2016-09-01
      • 2017-08-13
      • 2017-12-11
      • 2013-11-21
      • 2012-11-07
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多