设为首页 收藏本站
查看: 838|回复: 0

[经验分享] SQL Server计算Jaccard系数—sim(i,j)

[复制链接]

尚未签到

发表于 2018-10-13 09:05:37 | 显示全部楼层 |阅读模式
--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,null
  
SELECT name,column_id  into #test  FROM SYS.COLUMNS WHERE object_id =object_id('dbo.test')
  
and column_id>1
  
declare @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 int
  
create 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=0
  
select @min_id=min(id),@max_id=max(id),@global_min_id=min(id),@global_max_id=max(id) from Test
  
while(@min_id1
  
--select @str_union,@str_intersect,@column_id,@id_1,@id_2
  
if(@id_2=1)
  
   update Test_result set _1_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1
  
if(@id_2=2)
  
   update Test_result set _2_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1
  
if(@id_2=3)
  
   update Test_result set _3_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1
  
if(@id_2=4)
  
   update Test_result set _4_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1
  
if(@id_2=5)
  
   update Test_result set _5_= convert(numeric(10,4),len(@str_intersect)*1.0/len(@str_union)) where id=@id_1
  
if(@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+1
  
end
  
set @min_id=@min_id+1
  
end
  
drop table #test
  
drop table #a_union
  
drop 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,null
  
select * into #temp_test_table from test
  
select name,column_id into #tmp_test_columns from sys.columns where object_id=object_id('dbo.test') and column_id>1
  
declare @id int,@col_name varchar(20),@col_id int,@string_columns varchar(max),@sql_rs varchar(max),@num_1 int
  
create table #tmp_rs(num int)
  
while (select count(1) from #temp_test_table)>0
  
begin
  
select top 1 @id=id from #temp_test_table order by id
  
set @string_columns=''
  
while(select count(1) from #tmp_test_columns)>0
  
begin
  
select top 1 @col_name=name,@col_id=column_id from #tmp_test_columns order by column_id
  
select @sql_rs='select '+@col_name+' from test where id='+convert(varchar,@id)
  
insert into #tmp_rs
  
exec(@sql_rs)
  
select @num_1=num from #tmp_rs
  
if(@num_1=1)
  
set @string_columns=@string_columns+@col_name
  
delete from #tmp_test_columns where @col_name=name and @col_id=column_id
  
delete from #tmp_rs
  
end
  
insert into #tmp_test_columns select name,column_id  from sys.columns where object_id=object_id('dbo.test') and column_id>1
  
update test_str_column set str_columns=@string_columns where id =@id
  
delete from #temp_test_table where @id=id
  
end
  
drop table  #temp_test_table
  
drop table #tmp_test_columns
  
drop table #tmp_rs
  
select * from test
  
select * from test_str_column
  
select * from Test_result



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-620970-1-1.html 上篇帖子: 学习数据库Sql Server(1) 下篇帖子: SQL Server 2017 AlwaysOn AG 自动初始化(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表