网络文章/评论计数

时间:2014-07-02 18:46:12

标签: sql select web max average

如何在列表视图发生时列出前10条文章,这些文章对它们的评论最多。

SELECT  TOP 10 a.title as 'articles'
FROM    articles a 
JOIN    website_pageviews b on a.id = b.article_id
JOIN    comments c on a.id = c.commented_article_id 
GROUP BY a.id

我已经走到这一步了,但我现在有点失落 - 我如何做“平均”部分。

我的表格如下

table: website_pageviews (timestamp, article_id, visitor_id, and a bunch of others)
table: articles (id, title, timestamp, content, author, subject)
table: comments (commented_article_id, user_id, timestamp, text)

我在哪里使用最大值或平均值? 请帮忙......谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT  TOP 10 a.title as articles,
        count(c.commented_article_id) * 100 / count(b.visitor_id) as percentage
FROM    articles a 
JOIN    website_pageviews b on a.id = b.article_id
JOIN    comments c on a.id = c.commented_article_id 
GROUP BY a.id, a.title
order by count(c.commented_article_id) * 100 / count(b.visitor_id) desc