【问题标题】:Selecting the substring with the highest count in a column where the substrings are separated with spaces在子字符串用空格分隔的列中选择计数最高的子字符串
【发布时间】:2018-04-02 19:41:33
【问题描述】:

如何在单词以空格分隔的列中选择出现频率最高的单词?我使用 SQLite 作为数据库。

例如,

Column1  Column2
1        Apple Orange Banana
2        Strawberry Apple Pineapple
3        Grape Mango

期望的输出:苹果

【问题讨论】:

    标签: sqlite select count


    【解决方案1】:

    字数统计。假设您的表名为 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
    

    【讨论】:

    • 这很好用,忽略了查询生成器中的“无法解析查询文本”警告。谢谢。但是,当我再次打开它的查询生成器时,Visual Studio 的查询生成器会不断重新排列查询,但我可以始终只是将原始查询粘贴到某处并将其粘贴回查询生成器中。如果有人知道如何防止这种情况发生,如果您发布方法,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多