表H (有两个ID, L分别有6个不是0就是1的值)
ID L TF
A 1 0
A 2 0
A 3 0
A 4 1
A 5 1
A 6 1
B 1 1
B 2 1
B 3 1
B 4 0
B 5 0
B 6 0 表K (有多个ID,每个ID的 L分别也有6个不是0就是1的值)
ID L TF
I 1 1
I 2 1
I 3 1
I 4 0
I 5 0
I 6 1
K 1 0
K 2 0
K 3 0
K 4 1
K 5 1
K 6 0
M 1 1
M 2 0
M 3 0
M 4 1
M 5 1
M 6 1 请问我要算表1之各ID与表2的ID,誰最像(最小):
例如:表2的I与表1的A比较像,还是与表1的B比较像,值小就表示比较像,值的算法如下:
L=1时,(I的TF值-A的TF值)的平方+
L=2时,(I的TF值-A的TF值)的平方+
… +
L=6时,(I的TF值-A的TF值)的平方,总和为多少?要如何写呢?如果上面算出I与A的平方和比I与B的平方和小的话,那就将I与A放入同一表中,否则与A放入同一表中,结果新产生两表A_C、B_C如下:
表A_C
ID
A
I
K 表B_C
ID
B
M
ID L TF
A 1 0
A 2 0
A 3 0
A 4 1
A 5 1
A 6 1
B 1 1
B 2 1
B 3 1
B 4 0
B 5 0
B 6 0 表K (有多个ID,每个ID的 L分别也有6个不是0就是1的值)
ID L TF
I 1 1
I 2 1
I 3 1
I 4 0
I 5 0
I 6 1
K 1 0
K 2 0
K 3 0
K 4 1
K 5 1
K 6 0
M 1 1
M 2 0
M 3 0
M 4 1
M 5 1
M 6 1 请问我要算表1之各ID与表2的ID,誰最像(最小):
例如:表2的I与表1的A比较像,还是与表1的B比较像,值小就表示比较像,值的算法如下:
L=1时,(I的TF值-A的TF值)的平方+
L=2时,(I的TF值-A的TF值)的平方+
… +
L=6时,(I的TF值-A的TF值)的平方,总和为多少?要如何写呢?如果上面算出I与A的平方和比I与B的平方和小的话,那就将I与A放入同一表中,否则与A放入同一表中,结果新产生两表A_C、B_C如下:
表A_C
ID
A
I
K 表B_C
ID
B
M
解决方案 »
- 【开始日期】和【截止日期】处理问题
- sql server 2005 对去掉干扰词后某些词仍无法搜索
- 这个SQL怎么写?
- 我想把 .DB 的PARADOX 文件写入SERVER里面 但是使用以下语句老是出错~
- 在SQL Server和Access 中,如何把一个表所有字段中的空值转换成数值 0 ?
- 如何将sql2000的数据库转换成sql2005,并可以正常使用
- sql server大数据多表链接查询效率问题
- 触发器插入多行数据怎么写?
- 使用SQL SERVER6.5开发出现Protocol error in TDS stream
- 显式事务是否锁定数据库
- 请问如何知道修改一张表的内容会否影响其他表?
- 求一存儲過程
是 A与I比较,B与K比较
还是 A可以与I,K,M比较,B也可以与I,K,M比较 ?
--你看一下 这是你要的结果吗? 如果是的话你给的结果是错的。
create table h(ID varchar(2),L int,TF int)
insert into h
select 'A', 1, 0
union all select 'A', 2, 0
union all select 'A', 3, 0
union all select 'A', 4, 1
union all select 'A', 5, 1
union all select 'A', 6, 1
union all select 'B', 1, 1
union all select 'B', 2, 1
union all select 'B', 3, 1
union all select 'B', 4, 0
union all select 'B', 5, 0
union all select 'B', 6, 0
create table k(ID varchar(2),L int,TF int)
insert into k
select 'I', 1, 1
union all select 'I', 2, 1
union all select 'I', 3, 1
union all select 'I', 4, 0
union all select 'I', 5, 0
union all select 'I', 6, 1
union all select 'K', 1, 0
union all select 'K', 2, 0
union all select 'K', 3, 0
union all select 'K', 4, 1
union all select 'K', 5, 1
union all select 'K', 6, 0
union all select 'M ', 1, 1
union all select 'M', 2, 0
union all select 'M', 3, 0
union all select 'M', 4, 1
union all select 'M', 5, 1
union all select 'M', 6, 1 --插入A-C表的值
select m.id,m.name from
(select a.id, b.id name,he=sum(case when a.l=b.l then power((b.tf-a.tf),2) else 0 end ) from h a inner join k b on a.l=b.l where a.id ='a' group by a.id ,b.id) m inner join(select a.id, b.id name,he=sum(case when a.l=b.l then power((b.tf-a.tf),2) else 0 end ) from h a inner join k b on a.l=b.l where a.id ='b' group by a.id ,b.id )n
on m.name = n.name and m.he<n.he--插入B-C表的值
select n.id,n.name from
(select a.id, b.id name,he=sum(case when a.l=b.l then power((b.tf-a.tf),2) else 0 end ) from h a inner join k b on a.l=b.l where a.id ='a' group by a.id ,b.id) m inner join(select a.id, b.id name,he=sum(case when a.l=b.l then power((b.tf-a.tf),2) else 0 end ) from h a inner join k b on a.l=b.l where a.id ='b' group by a.id ,b.id )n
on m.name = n.name and m.he>n.he