sql表使用其他表测量

um6iljoc  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(291)

我在数据库里有两张table。这些表有一个id列。一个表总是准确的(表总是),另一个表有时是准确的(表有时)。表有时可能有多个idMap到表上的单个id。理想情况下,我希望在两个表之间有一个1-1Map,但事实并非如此。如何在成功Map(1-1)到table always的基础上,提出一种方法来度量table-times命中/准确度

vi4fp9gy

vi4fp9gy1#

您可以使用sql查询生成每个id的准确度报告:

select a.id, coalesce(s.cnt, 0) no_matches
from always a
left join (select id, count(*) cnt from sometimes group by id) s 
    on s.id = a.id

对于表中的每一行 always ,这将检查中有多少行匹配 sometimes . 任何值,除了 1 表示Map问题(完全不匹配或多个匹配)。
您可以生成摘要报告。假设您想要 a 在b中有适当的1-1匹配:

select avg(case when s.cnt = 1 then 1.0 else 0 end) accuracy_ratio
from always a
left join (select id, count(*) cnt from sometimes group by id) s 
    on s.id = a.id

也许你想产生三个比率:

select 
        avg(case when s.cnt = 1 then 1.0 else 0 end)     accuracy_ratio,
        avg(case when s.cnt is null then 1.0 else 0 end) unmatched_ratio,
        avg(case when s.cnt > 1 then 1.0 else 0 end)     many_matches_ratio,
from always a
left join (select id, count(*) cnt from sometimes group by id) s 
    on s.id = a.id

相关问题