2016-07-11 78 views
0

假設一個表t如下比較兩個不同的組列不同的值:SQL用於在一個表

 
    c1 c2 
    == == 
    a 1 
    a 2 
    b 1 
    b 2 
    b 3 
    c 4 
    c 2 

我們基本表由c1和具有三個組A,B,C。 我需要計算柱C2的相似兩組之間,如下所示:

 

    sim(a,b) = 2(common value of c2 are 1 and 2)/3(all value)=2/3 
    sim(b,c) = 1(b and c has only one value 2 in common)/4 = 1/4 
    sim(a,c) = 1/3 

我們可以使用SQL(的Oracle 11g語法第一)中,構建上述表達?

+0

標籤您與您正在使用的數據庫的問題。 –

回答

0

我相信這個查詢你想要做什麼:

select t1.c1, t2.c1, count(*) as NumInCommon, 
     (select count(distinct t.c2) 
     from t 
     where t.c1 in (t1.c1, t2.c1) 
     ) as NumInTotal 
from t t1 join 
    t t2 
    on t1.c2 = t2.c2 
group by t1.c1, t2.c1 
+0

這個在oracle運行的sql會顯示,按語法分組錯! –

相關問題