计算另一个表中存在的记录的比率

时间:2012-03-15 15:04:41

标签: sql sqlite

您可以帮我解决下面 SQLite 语句中的 unknownComputation 吗?

select t1.attr, unknownComputation as ratio
from t1
group by t1.attr
order by ratio

unknownComputation 应为(count(t1.attr) for which there exists t2.url = t1.link) / count(t1.attr)

并非来自t1的所有网址都在t2中,我想找到这些比率并按 t1.attr

对它们进行分组

2 个答案:

答案 0 :(得分:1)

根据您提供的信息,也许应该这样做:

select t1.attr, count(t2.url) / count(t1.attr) as ratio
from t1
left join t2 on t2.url = t1.link
group by t1.attr
order by ratio

然而,这认为一个t1可以有0或多个t2

答案 1 :(得分:1)

假设t1有一个名为id的唯一ID,请尝试:

select t1.attr, count(t2.url) / count(distinct t1.id) as ratio
from t1
left join t2 on t2.url = t1.link
group by t1.attr
order by 2