SQL查询以查找前5条累积推文

时间:2020-05-27 09:38:00

标签: mysql sql

如何查找累积推文数量最多的前5个月,并根据每个月的推文数量对其进行排序。

表twitter结构如下:

@pro

输出应该像这样

Token type  Month   count   Hash Tag Name
hashtag     200910  2   Babylove
hashtag     200911  2   babylove
hashtag     200912  90  babylove
hashtag     200812  100 mycoolwife
hashtag     200901  201 mycoolwife
hashtag     200910  1   mycoolwife
hashtag     200912  500 mycoolwife
hashtag     200905  23  abc
hashtag     200907  1000 abc

3 个答案:

答案 0 :(得分:2)

这是您需要的吗?

SELECT Month,SUM(count) as numtweets
FROM twitter 
GROUP BY Month
ORDER BY numtweets DESC
LIMIT 5

答案 1 :(得分:0)

我想如果使用SQL Management,应该是这样的:

SELECT TOP 5 Month, SUM(count) as number_tweets
FROM your_table_name
GROUP BY Month
ORDER BY number_Tweets DESC

但是,如果您将PHPmyadmin用于数据库,则应使用以下代码:

SELECT Month, SUM(count) as number_tweets
FROM your_table_name
GROUP BY Month
ORDER BY number_Tweets DESC
LIMIT 5

发生这种情况是因为PHPmyadmin不支持“ TOP”子句,我为我的数据库尝试了此代码,并且成功了

答案 2 :(得分:0)

您可以尝试以下操作

SELECT Token_type,Month,count, HashTag,cnt, SUM(cnt) AS cumulative
FROM (SELECT Token_type,Month,count, HashTag, COUNT(*) AS cnt
      FROM tweets
      GROUP BY Month) AS b
GROUP BY Month
ORDER BY cumulative DESC;