这是表result
的数据:
question_id is_correct count
64 TRUE 11
66 FALSE 20
77 FALSE 15
77 TRUE 9
我需要计算正确的费率,等于(TRUE_COUNT)/(TRUE_COUNT+FALSE_COUNT)
。现在的问题是一些问题错过了TRUE或FALSE数据,我想用零填充它以执行self join
操作并进行计算。有没有办法满足要求或填零?非常感谢!
预期结果:
question_id correct_rate
64 1
66 0
77 0.625
答案 0 :(得分:4)
不需要自联接,您可以进行条件聚合:
select
question_id,
-- to return a percentage
100.0 *
max(case when is_correct = TRUE then count else 0 end) /
sum(count)
-- otherwise needs to add a cast if "count" is an INT
-- max(case when is_correct = TRUE then count else 0 end) /
-- cast(sum(count) as decimal(5,4))
from tab
group by question_id
答案 1 :(得分:2)
条件聚合是正确的,但有一些细微差别:
unpersist
确保你不会以0除(最后为0/0),并确保你使用十进制或数字来获得所需的费率。
另请注意,您希望的.625的结果不正确的77,即.375 9真/ 24总
答案 2 :(得分:1)
您根本不需要JOIN
。请查看以下有关如何有条件地汇总数据的信息:
SELECT
question_id,
COALESCE(SUM(count) FILTER(WHERE is_correct),0) / SUM(count)::NUMERIC AS rate
FROM
result
GROUP BY
question_id;
-- Older version of PostgreSQL without FILTER:
SELECT
question_id,
COALESCE(SUM(CASE WHEN is_correct THEN count END),0) / SUM(count)::NUMERIC AS rate
FROM
result
GROUP BY
question_id;