在以空格分隔子串的列中选择计数最高的子串 [英] Selecting the substring with the highest count in a column where the substrings are separated with spaces
本文介绍了在以空格分隔子串的列中选择计数最高的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在用空格分隔单词的列中选择最常用的单词?我正在使用SQLite作为数据库.
How do I select the most frequent word in a column in which words are separated by spaces? I'm using SQLite as a database.
例如,
Column1 Column2
1 Apple Orange Banana
2 Strawberry Apple Pineapple
3 Grape Mango
所需的输出:Apple
Desired output: Apple
推荐答案
字数统计.假设您的表名为yourTable.使用公共表表达式(with子句)将Column2拆分为单独的单词.我从user1461607借来了一些知识,并提出了以下建议:
Word counting. Suppose your table is called yourTable. Use a common table expression (with clause) to split Column2 into separate words. I borrowed some knowledge from user1461607 and came up with this:
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
这篇关于在以空格分隔子串的列中选择计数最高的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文