【发布时间】:2021-03-29 09:37:24
【问题描述】:
我有一个查询,它计算一列中的所有单词,并给出单词的频率和频率等级作为结果。出于某种原因,我不断得到一个没有字的行。我该如何摆脱它?
表:
CREATE TABLE dummy (
created_at TIMESTAMPTZ,
tweet TEXT);
插入:
INSERT INTO dummy VALUES ('2020-12-18 00:00:00+00', 'foo squared');
INSERT INTO dummy VALUES ('2020-12-18 00:00:00+00', 'foo foo');
INSERT INTO dummy VALUES ('2020-12-18 00:00:00+00', 'foo foo');
INSERT INTO dummy VALUES ('2020-12-18 00:00:00+00', 'foo bar');
查询:
select *
from (
select date_trunc('day', created_at) as created_day, word, count(*) as cnt,
rank() over(partition by date_trunc('day', created_at) order by count(*) desc) rn
from dummy d
cross join lateral regexp_split_to_table(
regexp_replace(tweet, '\y(rt|co|https|bar|none)\y', '', 'g'),
'\s+'
) w(word)
group by created_day, word
) d
where created_day = CURRENT_DATE and word IS NOT NULL
order by rn
LIMIT 10;
返回:
created_day | word | cnt | rn
------------------------+---------+-----+----
2020-12-18 00:00:00+00 | foo | 4 | 1
2020-12-18 00:00:00+00 | | 2 | 2
2020-12-18 00:00:00+00 | arm | 1 | 3
2020-12-18 00:00:00+00 | squared | 1 | 3
我想去掉空白词:
created_day | word | cnt | rn
------------------------+---------+-----+----
2020-12-18 00:00:00+00 | foo | 4 | 1
2020-12-18 00:00:00+00 | arm | 1 | 2
2020-12-18 00:00:00+00 | squared | 1 | 3
【问题讨论】:
-
您能否提供一个最低限度的可重现示例,以便调查问题?另外,鉴于您要提出一个新问题,我建议您在 your previous question 上接受答案。
-
抱歉,我已更新问题以包含可重现的示例
标签: sql postgresql count greatest-n-per-group lateral-join