Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设一个表 t 如下:
c1 c2 == == 一个 1 a2 乙 1 b 2 b 3 c 4 2
我们将该表按 c1 分组,并具有三个组 a、b、c。我需要计算两组之间 c2 列的相似性,如下所示:
sim(a,b) = 2(c2的共同值是1和2)/3(所有值)=2/3 sim(b,c) = 1(b 和 c 只有一个共同的值 2)/4 = 1/4 sim(a,c) = 1/3
我们可以使用sql(Oracle 11g语法优先)来构造上面的表达式吗?
我相信这个查询可以满足您的要求:
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