Facebook
From Kadir Çallıoğlu, 1 Month ago, written in SQL.
Embed
Download Paste or View Raw
Hits: 130
  1. CREATE TEMPORARY TABLE median_vaccinations AS
  2. SELECT country,
  3.        AVG(daily_vaccinations) AS median_vaccination
  4. FROM (
  5.     SELECT country,
  6.            daily_vaccinations,
  7.            COUNT(*) OVER (PARTITION BY country) AS total_rows,
  8.            ROW_NUMBER() OVER (PARTITION BY country ORDER BY daily_vaccinations) AS row_num
  9.     FROM country_vaccination_stats
  10.     WHERE daily_vaccinations IS NOT NULL
  11. ) t
  12. WHERE row_num IN (total_rows / 2, total_rows / 2 + 1, 1)
  13. GROUP BY country;
  14.  
  15. UPDATE country_vaccination_stats
  16. SET daily_vaccinations = COALESCE((SELECT median_vaccination FROM median_vaccinations WHERE median_vaccinations.country = country_vaccination_stats.country), 0)
  17. WHERE daily_vaccinations IS NULL;