sql查询查找前5个累计tweet

whlutmcx  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(325)

如何找到累计微博数量最多的前5个月,并根据每个月的微博数量进行排序。
表结构如下:

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

输出应该是这样的

month  numtweets
200907  1000
200912  590
200901  201
200812  100
200905  23
pgvzfuti

pgvzfuti1#

这是你需要的吗?

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

dxxyhpgq2#

您可以尝试以下方法

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;
omqzjyyz

omqzjyyz3#

如果使用sql管理,我想应该是这样的:

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”子句,我为我的数据库尝试了这段代码,结果成功了

相关问题