【发布时间】:2018-04-02 19:41:33
【问题描述】:
如何在单词以空格分隔的列中选择出现频率最高的单词?我使用 SQLite 作为数据库。
例如,
Column1 Column2
1 Apple Orange Banana
2 Strawberry Apple Pineapple
3 Grape Mango
期望的输出:苹果
【问题讨论】:
如何在单词以空格分隔的列中选择出现频率最高的单词?我使用 SQLite 作为数据库。
例如,
Column1 Column2
1 Apple Orange Banana
2 Strawberry Apple Pineapple
3 Grape Mango
期望的输出:苹果
【问题讨论】:
字数统计。假设您的表名为 yourTable。使用公用表表达式(with 子句)将 Column2 拆分为单独的单词。我从 user1461607 那里借了一些知识,想出了这个:
WITH RECURSIVE split(word, str, hasspace) AS (
SELECT '', Column2, 1 from yourTable
UNION ALL SELECT
substr(str, 0,
case when instr(str, ' ')
then instr(str, ' ')
else length(str)+1 end),
ltrim(substr(str, instr(str, ' ')), ' '),
instr(str, ' ')
FROM split
WHERE hasspace
)
SELECT trim(word) FROM split WHERE word!='' GROUP BY trim(word) ORDER BY count(*) DESC LIMIT 1
【讨论】: