表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 


K 表B_C 
ID 

M

解决方案 »

  1.   

    看了半天没大看明白什么意思。
    是 A与I比较,B与K比较
    还是 A可以与I,K,M比较,B也可以与I,K,M比较 ?
      

  2.   

    是的,A可以与I,K,M比较,B也可以与I,K,M比较 ,值小就表示比较像
      

  3.   


    --你看一下 这是你要的结果吗? 如果是的话你给的结果是错的。
    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