-- Create a temporary table to store median values for each country 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; -- Update entries in the original table where daily_vaccinations is missing using the medians 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); -- Set daily_vaccinations to 0 for countries with no valid records at all UPDATE vaccinations SET daily_vaccinations = 0 WHERE daily_vaccinations IS NULL;