我有两个表,我想获取quality_score_A和Quality_score_B的平均quality_score。
这是我尝试过的方法,但这使我在quality_score_a和quality_score_b中具有相同的值
SELECT AVG (quality_score),AVG (quality_score)
FROM REVIEW
JOIN Score_table on score.quality_score_A
JOIN Score_table on score.quality_score_B
WHERE PRODUCT_ID = 2
GROUP BY PRODUCT_ID;
请参阅附件表的布局和所需的结果
答案 0 :(得分:0)
您需要指定要加入的条件:
SELECT product_id, AVG(a.quality_score), AVG(b.quality_score)
FROM review r
JOIN score_table a ON r.quality_score_a = a.score_id
JOIN score_table b ON r.quality_score_a = b.score_id
WHERE product_id = 2
GROUP BY product_id
答案 1 :(得分:0)
我认为最简单的方法是取消结果:
select r.product_id,
avg(case when which = 'a' then s.quality_score end) as a_avg,
avg(case when which = 'b' then s.quality_score end) as b_avg
from ((select r.product_id, quality_score_a as score_id, 'a' as which
from reviews r
) union all
(select r.product_id, quality_score_b as score_id, 'b' as which
from reviews r
)
) r join
scores s
on r.score_id = s.score_id
group by r.product_id