【发布时间】:2021-03-28 22:25:18
【问题描述】:
我有一个存储 twitter 数据的数据库:
Create Table tweet(
ID BIGINT UNIQUE,
user_ID BIGINT,
created_at TIMESTAMPTZ,
tweet TEXT;
我正在尝试编写一个查询,该查询通过 tweet 中的所有行获取每个单词的频率,并返回前十个最常见的单词以及单词在每个日期的排名。
例子:
("word1":[1,20,22,23,24,25,26,27,28,29,30,29,28,27,26,25,26,27,28,29,30,29,28,29,28,27,28,29,30,30,...],
'word2' [...])
我当前的查询获得了前十个单词,但我在获取这些单词每天的排名时遇到了一些问题。
当前查询:
SELECT word, count(*)
FROM (
SELECT regexp_split_to_table(
regexp_replace(tweet_clean, '\y(rt|co|https|amp|f)\y', '', 'g'), '\s+')
AS word
FROM tweet
) t
GROUP BY word
ORDER BY count(*) DESC
LIMIT 10;
返回:
[('vaccine', 286669),
('covid', 213857),
('yum', 141345),
('pfizer', 39532),
('people', 28960),
('beer', 27117),
('say', 24569),
('virus', 23682),
('want', 21988),
('foo', 19823)]
【问题讨论】:
-
您可以使用
rank()或dense_rank()获取排名。
标签: sql postgresql count greatest-n-per-group lateral-join