[英]Compare two relations with postgresql
我正在通過連接表比較兩個條目並返回匹配的百分比
聯接表如下所示:
|recipe_id|ingredient_id|
|---------|-------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
|-----------------------|
| 2 | 4 |
| 2 | 10 |
| 2 | 20 |
|-----------------------|
如果我提供了一個數組ingredient_id
=> [1,2,3]
,則預期結果為
|recipe_id|match_percent|
|---------|-------------|
| 1 | 100 |
|-----------------------|
| 2 | 0 |
|-----------------------|
我該如何實現?
我的建議是將數組作為查找表,並與之連接以查找計數
select t.recipe_id,
count(t.ingredient_id)/(select count(*) from lookup_table)*100 as match_percent
from
(
select t1.recipe_id,t1.ingredient_id from table as t1 inner join lookup_table as t2
on t1.ingredient_id=t2.ingredient_id
) as t
group by t1.recipe_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.