Вам нужно использовать либо CTE для установки набора данных перед применением деления, либо дублировать предложение SUM()
в делении.
WITH dataset AS
(
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
, c.population
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
ORDER BY c.name ASC;
)
SELECT *
, (total_affected / population) as rate_of_infection <-- Something like this line
FROM dataset
;
Или:
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
, (SUM(COALESCE(cured, 0) + COALESCE(dead, 0) + infected)/ c.population) as rate_of_infection
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
;