我想根据我所做的转换来分离彩色作业:
select count(ap.id),
(count(ap.id)/cast(SUM(j.views) as float) * 100) as conversion,
j.company_id
from applications ap
right join jobs j
on ap.job_id = j.id
where j.company_id = 61805
group by j.id
如果转换列大于该结果总平均值的75%,我想创建新的别名列,值为绿色。如果转换列介于35%和75%之间,则列值将黄色且小于35%,列值将红色。
如上所述我可以提到postgres吗?提前谢谢。
答案 0 :(得分:1)
窗口功能是你的朋友:
SELECT count,
conversion,
CASE
WHEN conversion > avg_conv * 0.75
THEN 'green'
WHEN conversion < avg_conv * 0.35
THEN 'red'
ELSE 'yellow'
END AS color,
company_id
FROM (SELECT count,
conversion,
avg(conversion) OVER () AS avg_conv,
company_id
FROM (SELECT count(ap.id),
(count(ap.id)/cast(SUM(j.views) as float) * 100) AS conversion,
j.company_id
FROM applications ap
RIGHT JOIN jobs j
ON ap.job_id = j.id
WHERE j.company_id = 61805
GROUP BY j.company_id
) with_avg
) with_color;
(未测试的)