提问人:Goutham mano 提问时间:3/26/2021 最后编辑:Chris SchallerGoutham mano 更新时间:3/27/2021 访问量:74
Sql Server 查找字符串的出现次数
Sql server finding the number of occurence of a string
问:
如何使用sql server/Tsql找到一个句子中的出现次数?
在以下字符串值中,我想查找多次出现的单词数:
“我的良心有一千种舌头,每一种舌头都带来了几个故事,每个故事都谴责我是一个恶棍。”
在上面的句子中,“every”和“several”出现了两次,如何在没有硬代码的情况下提取这些单词
答:
0赞
Stu
3/26/2021
#1
有太多的“假设”来写一些完全满足你所有可能要求的东西,但是以下内容应该给你一个工作的想法:
select [value]
from String_Split('My conscience hath a thousand several tongues, And every tongue brings in a several tale, And every tale condemns me for a villain',' ')
group by value having count(*)>1
value
--------
a
And
every
several
1赞
iamdave
3/26/2021
#2
这只是入门的一种方式,但请注意,这远非一个完整的解决方案,因为您需要定义单词的构成。这看似简单,但实际上非常困难。
例如,在我下面的脚本和输出中,您可以看到由于在空格字符上拆分而被视为一个单词。words,
所以去掉标点符号吧!很简单,对吧?那么,您将如何确保删除单词开头和结尾的单引号,例如忽略缩略词中的单引号?a sentence where a 'word' is quoted
don't replace this one
您将需要进行大量的预处理,或者接受您在这里尝试实现的任何内容都会得到很多不正确的输出。
查询
declare @t table(txt varchar(500));
insert into @t values
('My conscience hath a thousand several tongues, And every tongue brings in a several tale, And every tale condemns me for a villain.')
,('Another test string with no repeated words so that there are more values to show how this could work on an entire dataset')
,('And another that does repeat words, so that those words, where they match can be aggregated')
;
select t.txt
,s.[value] as word
,count(s.[value]) as occurances
from @t as t
cross apply string_split(t.txt,' ') as s
group by t.txt
,s.[value]
having count(s.[value]) > 1;
输出
txt的 | 词 | 发生率 |
---|---|---|
我的良心有一千种舌头,每一种舌头都带来了几个故事,每个故事都谴责我是一个恶棍。 | 一个 | 3 |
我的良心有一千种舌头,每一种舌头都带来了几个故事,每个故事都谴责我是一个恶棍。 | 和 | 2 |
我的良心有一千种舌头,每一种舌头都带来了几个故事,每个故事都谴责我是一个恶棍。 | 每 | 2 |
我的良心有一千种舌头,每一种舌头都带来了几个故事,每个故事都谴责我是一个恶棍。 | 几个 | 2 |
另一个是重复单词的,以便可以聚合这些单词,它们匹配的地方 | 那 | 2 |
另一个是重复单词的,以便可以聚合这些单词,它们匹配的地方 | 的话 | 2 |
评论
0赞
Goutham mano
3/26/2021
没有string_split功能可以吗?
0赞
iamdave
3/26/2021
您将需要某种字符串拆分函数,可以是内置版本,也可以是您自己添加到数据库中的版本(如果不可用)。有很多关于如何在线执行此操作的例子。
0赞
Navid Anjum
3/26/2021
#3
对不起,我之前误解了你的问题,所以我正在修改我的答案。 首先创建将接受字符串作为参数的存储例程。
DELIMITER //
create function `find_occurances`(string varchar(192))
returns varchar(192) deterministic
begin
declare substr varchar(191) default "";
declare occurances int default 0;
declare length int default 0;
declare temp_str varchar(191) default string;
DELETE FROM occurances_in_sentence;
create temporary table if not exists occurances_in_sentence(value varchar(51),occurances int);
set length = round((length(string) - length(Replace(string," ","")))/1);
while length >= 0 do
set substr = SUBSTRING_INDEX(string," ",1);
if(length(substr) = 0)then
return substr;
end if;
set occurances=round((length(string) - length(Replace(string,substr,"")))/length(substr));
insert into occurances_in_sentence values (substr,occurances);
set string = Replace(string,substr,"");
set string = trim(string);
set length = length -1;
end while;
return 1;
end//
DELIMITER ;
成功创建函数后。使用字符串调用函数。
select find_occurances("This is testing of every occurance in sentence of every number") as sub;
它的作用是创建一个结果的临时表。
select * from occurances_in_sentence;
评论
0赞
Stu
3/26/2021
这难道不需要你既知道你在寻找什么,又需要提前进行硬编码吗?这不是OP所要求的。
0赞
Navid Anjum
3/27/2021
我已经更改了答案,现在您甚至可以运行它。
0赞
iamdave
3/30/2021
与基于集合的方法相比,为此使用循环将具有糟糕的性能。while
评论
four lines of sentence
请定义句子,并且是一行或四行中的四个句子。 定义单词:标点符号、数字、任何其他字符呢?你将不得不更清楚地了解你拥有哪些数据以及你想用它做什么,请编辑你的问题并详细说明varchar
the number of repeated words