2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2 3 4 5 6 7 Jamaica United States ... Syrian Arab R... Afghanistan Italy Egypt Armenia Australia and ... Denmark Azerbaijan United Kingd... United Repub... Chad Saudi Arabia Indonesia 0 20 40 60 80 WITH cte1 AS ( SELECT year, country, AVG(loss_percentage) AS total_loss_percentage, ROW_NUMBER() OVER (PARTITION BY year ORDER BY AVG(loss_percentage) DESC) AS row_num FROM food WHERE year >= 2016 GROUP BY year,country HAVING COUNT(country) >= 3 ), cte2 AS ( SELECT year, country, commodity, AVG(loss_percentage) AS avg_loss_percentage, ROW_NUMBER() OVER (PARTITION BY year, country ORDER BY AVG(loss_percentage) DESC) AS row_num FROM food WHERE year >= 2016 GROUP BY year, country, commodity ) SELECT cte1.year, cte1.country, cte2.commodity, to_char(cte1.total_loss_percentage, 'fm999G999D99') AS avg_l_p FROM cte1 JOIN cte2 ON cte1.year = cte2.year AND cte1.country = cte2.country WHERE cte1.row_num <= 5 AND cte2.row_num = 1; Serbia Latvia Uzbekistan The former Yu... Estonia Hungary Ukraine Mongolia Jordan Ecuador India Bolivia (Plurin... Georgia Romania Kazakhstan 0 2 4 6 8 WITH cte AS ( SELECT year, country, AVG(loss_percentage) AS total_loss_percentage, ROW_NUMBER() OVER (PARTITION BY year ORDER BY AVG(loss_percentage) ASC) AS row_num FROM food WHERE year >= 2016 GROUP BY year,country HAVING COUNT(country) >= 3 ) SELECT year, country, CASE WHEN LEFT(to_char(total_loss_percentage,'fm999G999D99'),1) = ',' THEN '0' || to_char(total_loss_percentage,'fm999G999D99') ELSE to_char(total_loss_percentage,'fm999G999D99') END AS tot_loss_percentage FROM cte WHERE row_num <= 5 ORDER BY year, total_loss_percentage ASC; Carrots and tur... Lettuce and chi... Cabbages Tomatoes Cauliflowers an... Broad beans an... Potatoes Sweet potatoes Other vegetable... Cantaloupes an... Onions and sh... Maize (corn) Wheat Apples Grapes 0 20 40 60 80 WITH cte AS ( SELECT year, commodity, AVG(loss_percentage) AS total_loss_percentage, ROW_NUMBER() OVER (PARTITION BY year ORDER BY AVG(loss_percentage) DESC) AS row_num FROM food WHERE year >= 2016 GROUP BY year, commodity HAVING COUNT(commodity) >= 3) SELECT cte.year, cte.commodity, to_char(total_loss_percentage,'fm999G999D99') AS loss_perc, (SELECT food_supply_stage FROM food WHERE cte.year = food.year AND cte.commodity = food.commodity ORDER BY loss_percentage DESC LIMIT 1) AS food_supply_stage FROM cte WHERE row_num <= 3 ORDER BY year, loss_perc DESC;