SQL Server计算Jaccard系数—sim(i,j)
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,前些天,在Q群里面看到有人请教这样一个问题:在SQL Server中如何用SQL实现以下计算由图得知,该问题是如何计算Jaccard系数。Jaccard系数,又称为Jaccard相似系数(Jaccar
千家信息网最后更新 2025年02月02日SQL Server计算Jaccard系数—sim(i,j)
前些天,在Q群里面看到有人请教这样一个问题:在SQL Server中如何用SQL实现以下计算
由图得知,该问题是如何计算Jaccard系数。Jaccard系数,又称为Jaccard相似系数(Jaccard similarity coefficient)用于比较有限样本集之间的相似性与差异性。Jaccard系数值越大,样本相似度越高。
SQL Server通过intersect实现交集,union实现并集,如下:
intersect交集
计算交集代码段如下:(1 intersect 0 = null,1 intersect 1 = 1,0 intersect 0 = 0)
union并集
计算并集代码段如下:(1 union 0 = 1 0,1 union 1 = 1,0 union 0 = 0)
原理简介:
通过sys.columns表获取表的字段名与字段ID,循环字段名,并根据字段名去取到表中对应的值:如字段名为A,那么取ID为1的值:select A from test where id=1,取ID为2的值:select A from test where id=2,
然后将2个值进行交集与并集,字段循环结束后,得到@str_intersect,@str_union,计算比值:len(@str_intersect)*1.0/len(@str_union)
最终结果如下:
全部代码如下:(建表,见注释部分)
--create table Test(id int,A INT,B INT,C INT,D INT,E INT,F INT)--INSERT INTO TEST SELECT 1,1,1,1,0,0,0--INSERT INTO TEST SELECT 2,0,1,0,1,0,1--INSERT INTO TEST SELECT 3,1,1,1,1,0,1--INSERT INTO TEST SELECT 4,1,1,1,0,1,0--INSERT INTO TEST SELECT 5,0,1,0,1,1,1--INSERT INTO TEST SELECT 6,0,0,1,0,1,1--drop table Test_result--create table Test_result(id int,_1_ numeric(10,4),_2_ numeric(10,4),_3_ numeric(10,4),_4_ numeric(10,4),_5_ numeric(10,4),_6_ numeric(10,4))--insert into Test_result select 1,null,null,null,null,null,null--insert into Test_result select 2,null,null,null,null,null,null--insert into Test_result select 3,null,null,null,null,null,null--insert into Test_result select 4,null,null,null,null,null,null--insert into Test_result select 5,null,null,null,null,null,null--insert into Test_result select 6,null,null,null,null,null,nullSELECT name,column_id into #test FROM SYS.COLUMNS WHERE object_id =object_id('dbo.test')and column_id>1declare @id_1 int=0,@id_2 int=0,@str_union varchar(max),@a_union int,@sql_union varchar(max),@str_intersect varchar(max),@a_intersect int,@sql_intersect varchar(max)declare @name varchar(20),@column_id intcreate table #a_union(num int)create table #a_intersect(num int)declare @min_id int=0,@max_id int=0,@global_min_id int=0,@global_max_id int=0select @min_id=min(id),@max_id=max(id),@global_min_id=min(id),@global_max_id=max(id) from Testwhile(@min_id<=@max_id)beginselect @id_1=@min_id,@id_2=@global_min_idwhile @id_2<=@global_max_idbeginselect @str_union='',@str_intersect=''while(select count(1) from #test)>0 beginselect top 1 @name=name,@column_id=column_id from #test order by column_idselect @sql_union='select CASE '+'WHEN (select count(1) from (SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_1)+'union SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_2)+') as a)>1 THEN 1'+' WHEN ISNULL((SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_1)+' UNION SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_2)+'),0)=0 THEN 0 ELSE 1 END'insert into #a_unionexec(@sql_union)SELECT @a_union=num from #a_uniondelete from #a_unionif(@a_union=1)select @str_union+=@nameselect @sql_intersect='select CASE WHEN ISNULL((SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_1)+' INTERSECT SELECT '+@name+' FROM TEST WHERE ID='+convert(varchar,@id_2)+'),0)=0 THEN 0 ELSE 1 END'insert into #a_intersectexec(@sql_intersect)SELECT @a_intersect=num from #a_intersectdelete from #a_intersectif(@a_intersect=1)select @str_intersect+=@namedelete from #test where @name=name and @column_id=column_idendinsert into #test SELECT name,column_id FROM SYS.COLUMNS WHERE object_id =object_id('dbo.test')and column_id>1--select @str_union,@str_intersect,@column_id,@id_1,@id_2if(@id_2=1) update Test_result set _1_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1if(@id_2=2) update Test_result set _2_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1if(@id_2=3) update Test_result set _3_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1if(@id_2=4) update Test_result set _4_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1if(@id_2=5) update Test_result set _5_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1if(@id_2=6) update Test_result set _6_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1 set @id_2=@id_2+1endset @min_id=@min_id+1enddrop table #testdrop table #a_uniondrop table #a_intersect-------------create table test_str_column(id int,str_columns varchar(max))--insert into test_str_column select 1,null--insert into test_str_column select 2,null--insert into test_str_column select 3,null--insert into test_str_column select 4,null--insert into test_str_column select 5,null--insert into test_str_column select 6,nullselect * into #temp_test_table from testselect name,column_id into #tmp_test_columns from sys.columns where object_id=object_id('dbo.test') and column_id>1declare @id int,@col_name varchar(20),@col_id int,@string_columns varchar(max),@sql_rs varchar(max),@num_1 intcreate table #tmp_rs(num int)while (select count(1) from #temp_test_table)>0beginselect top 1 @id=id from #temp_test_table order by idset @string_columns=''while(select count(1) from #tmp_test_columns)>0beginselect top 1 @col_name=name,@col_id=column_id from #tmp_test_columns order by column_idselect @sql_rs='select '+@col_name+' from test where id='+convert(varchar,@id)insert into #tmp_rsexec(@sql_rs)select @num_1=num from #tmp_rsif(@num_1=1)set @string_columns=@string_columns+@col_namedelete from #tmp_test_columns where @col_name=name and @col_id=column_iddelete from #tmp_rsendinsert into #tmp_test_columns select name,column_id from sys.columns where object_id=object_id('dbo.test') and column_id>1update test_str_column set str_columns=@string_columns where id =@iddelete from #temp_test_table where @id=idenddrop table #temp_test_tabledrop table #tmp_test_columnsdrop table #tmp_rsselect * from testselect * from test_str_columnselect * from Test_result
字段
交集
系数
相似
代码
样本
问题
循环
之间
原理
差异
差异性
数值
有限
比值
注释
相似性
简介
结果
部分
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
怎么开代理服务器
java数据库驱动设计模式
饥荒开专用服务器
网络安全在大数据的应用
捕蝇纸下载软件开发
创造与魔法墨颜在哪个服务器
我的世界服务器被封号
嵌入式软件开发陈文智pdf
安阳市网络安全运维
腾讯网络安全总监
2u服务器散热器批发商
信息与网络安全部岗位职责
不同网络服务器怎么传输数据
新人民银行软件开发中心
FDT软件开发工具
比2少1的数据库
金融网络安全法全文
金盾6942服务器机柜
泉州互联网科技有限公司
网络安全服务手抄报画
考三级网络技术有什么用
家乡宣传网络安全
王者集结更新后找不到原来服务器
阿里云服务器怎么开电脑桌面
怎么保证项目的网络安全
关乎网络技术(上海)有限公司
苹果与服务器建立安全连接
网络安全运营指标
索引数据库包括
bbs网络技术