Python 基于前x个字符数的SQLite计数相似项

Python 基于前x个字符数的SQLite计数相似项,python,sqlite,Python,Sqlite,我有一个错误表,看起来像 "file not configured [filename1]" "file not configured [filename2]" "file not configured [filename3]" "A bomb went off!!" 我想做的是得到一个错误发生的列表 text occurrences ____________________________________ file not configured

我有一个错误表,看起来像

"file not configured [filename1]"
"file not configured [filename2]"
"file not configured [filename3]"
"A bomb went off!!"
我想做的是得到一个错误发生的列表

text                      occurrences   
____________________________________
file not configured..     3
a bomb went off!!         1

是否可以在sqlite中运行查询以检查前x个字符的相似性?

您可以尝试聚合每个错误消息的前一定数量的字符:

SELECT
    SUBSTR(text, 1, 12) AS text,   -- or however long a substring you want
    COUNT(*) AS occurrences
FROM errors
GROUP BY
    SUBSTR(text, 1, 12);

请注意,上述查询严格不符合ANSI标准,因为GROUPBY子句包含一个列函数。要解决这个问题,我们可以用子查询重写。

按文本分组不符合ANSI标准吗?是的,但我不确定按全文分组,但选择子字符串是否有效。请使用与实际列名称不同的别名,并使用按别名分组