计算匹配词的百分比
Calculate percentage of matching word
CREATE TABLE tbl_pat
(
id int,
name varchar(100),
[address] varchar(500)
);
INSERT INTO tbl_pat VALUES(1,'Jack','Lane 1, 90 Road Street, SL');
INSERT INTO tbl_pat VALUES(2,'Will','SA, Lane 10, Street road');
INSERT INTO tbl_pat VALUES(3,'White','Lane 1 ZIM');
INSERT INTO tbl_pat VALUES(4,'Shaw','Street Road');
INSERT INTO tbl_pat VALUES(5,'Steve','Road Street');
INSERT INTO tbl_pat VALUES(6,'Brown','Nz Road 10');
预期结果:
搜索字符串是:Street Road
Name Address Percentage
---------------------------------------------
Shaw Street Road 100
Steve Road Street 100
Will SA, Lane 10, Street road 20
Jack Lane 1, 90 Road Street, SL 17
注意:百分比是假设提到的,但前两个应该是 100% 百分比,因为它是完全匹配的。
我正在使用 PATINDEX 搜索单词。
查询:正在搜索 街道
SELECT [Name],[Address]
FROM tbl_pat
WHERE PATINDEX('%Street%',[Address])>=1 AND PATINDEX('%Road%',[Address])>=1
如何计算单个select语句中匹配词的百分比?
我在这里使用 SQL Server 2017
中的 string_split()
。您可以替换为任何可用的字符串拆分函数。搜索一下
它并不完美,但它适用于您的样本。
select p.id, p.name, p.address, count(k.value) * 100.0 / count(*) as pecentage
from tbl_pat p
cross apply string_split(replace([address], ',', ' '), ' ') w
left join
(
select value
from string_split ('Road Street', ' ')
) k on w.value = k.value
group by p.id, p.name, p.address
我认为您提供的预期百分比计算有问题
例如 Jack,匹配 2 个输出 6 个单词。所以我希望它的结果是 %33
请测试以下 SQL 查询,其中我使用 String_Split function 将文本拆分为单词
declare @str nvarchar(max) = 'Road Street'
; with tbl as (
select *, count(*) over (partition by id) word_count
from tbl_pat t
cross apply STRING_SPLIT(replace(t.address,',',' '), ' ')
where trim([value]) <> ''
)
select distinct id, [name], word_count, count(search.[value]) over (partition by id),
convert( decimal(5,2), (100.0 * (count(search.[value]) over (partition by id)) / word_count))
from tbl
left join (
select * from STRING_SPLIT(@str, ' ')
) search
on search.[value] = tbl.[value]
order by id
输出是
CREATE TABLE tbl_pat
(
id int,
name varchar(100),
[address] varchar(500)
);
INSERT INTO tbl_pat VALUES(1,'Jack','Lane 1, 90 Road Street, SL');
INSERT INTO tbl_pat VALUES(2,'Will','SA, Lane 10, Street road');
INSERT INTO tbl_pat VALUES(3,'White','Lane 1 ZIM');
INSERT INTO tbl_pat VALUES(4,'Shaw','Street Road');
INSERT INTO tbl_pat VALUES(5,'Steve','Road Street');
INSERT INTO tbl_pat VALUES(6,'Brown','Nz Road 10');
预期结果:
搜索字符串是:Street Road
Name Address Percentage
---------------------------------------------
Shaw Street Road 100
Steve Road Street 100
Will SA, Lane 10, Street road 20
Jack Lane 1, 90 Road Street, SL 17
注意:百分比是假设提到的,但前两个应该是 100% 百分比,因为它是完全匹配的。
我正在使用 PATINDEX 搜索单词。
查询:正在搜索 街道
SELECT [Name],[Address]
FROM tbl_pat
WHERE PATINDEX('%Street%',[Address])>=1 AND PATINDEX('%Road%',[Address])>=1
如何计算单个select语句中匹配词的百分比?
我在这里使用 SQL Server 2017
中的 string_split()
。您可以替换为任何可用的字符串拆分函数。搜索一下
它并不完美,但它适用于您的样本。
select p.id, p.name, p.address, count(k.value) * 100.0 / count(*) as pecentage
from tbl_pat p
cross apply string_split(replace([address], ',', ' '), ' ') w
left join
(
select value
from string_split ('Road Street', ' ')
) k on w.value = k.value
group by p.id, p.name, p.address
我认为您提供的预期百分比计算有问题 例如 Jack,匹配 2 个输出 6 个单词。所以我希望它的结果是 %33
请测试以下 SQL 查询,其中我使用 String_Split function 将文本拆分为单词
declare @str nvarchar(max) = 'Road Street'
; with tbl as (
select *, count(*) over (partition by id) word_count
from tbl_pat t
cross apply STRING_SPLIT(replace(t.address,',',' '), ' ')
where trim([value]) <> ''
)
select distinct id, [name], word_count, count(search.[value]) over (partition by id),
convert( decimal(5,2), (100.0 * (count(search.[value]) over (partition by id)) / word_count))
from tbl
left join (
select * from STRING_SPLIT(@str, ' ')
) search
on search.[value] = tbl.[value]
order by id
输出是