I want to separate jobs with color based on conversion of what I did as follow:
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
if conversion column is greater than 75% of total average of that result, I want to create new alias column and value will be green . if conversion column is between 35% and 75%, column value will be yellow and less than 35%, column value will be red .
Is can be possible do in postgres as above query I've mentioned? Thanks in advance.
Window functions are your friend:
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;
(untested)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.