--1 DROP TABLE IF EXISTS CountryMedians; CREATE TEMPORARY TABLE CountryMedians AS SELECT country, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY daily_vaccinations) AS median_vaccinations FROM vaccinations WHERE daily_vaccinations IS NOT NULL GROUP BY country; --2 UPDATE vaccinations SET daily_vaccinations = (SELECT median_vaccinations FROM CountryMedians WHERE CountryMedians.country = vaccinations.country) WHERE daily_vaccinations IS NULL AND EXISTS (SELECT 1 FROM CountryMedians WHERE CountryMedians.country = vaccinations.country); --3 UPDATE vaccinations SET daily_vaccinations = 0 WHERE daily_vaccinations IS NULL;