How would I use the column I defined as 'total_affected' in place of the long function?.. I feel like there is a better way to do this rather than to repeat the same thing from the previous line. I am not sure how to define it so I can use it right after in the next column.
SELECT c.name,
COALESCE(sum(country_status_count.cured), 0) as cured,
COALESCE(sum(country_status_count.dead), 0) as dead,
cic.infected,
SUM(COALESCE(cured, 0) + COALESCE(dead, 0) + infected) AS total_affected,
(total_affected / c.population) as rate_of_infection <-- Something like this line instead of copying the whole function from the line above
FROM country_status_count
RIGHT JOIN country c ON country_status_count.country_id = c.id
RIGHT JOIN country_infection_counts cic ON c.name = cic.name
GROUP BY c.name, cic.infected, c.population
ORDER BY c.name ASC;