Top Ingredient by Continent

WITH IngredientCounts AS (
    SELECT 
        cr.Continent,
        i.Ingredient AS Continent_Ingredient,
        COUNT(*) AS Top_Ingredient_count,
        DENSE_RANK() OVER (PARTITION BY cr.Continent ORDER BY COUNT(*) DESC) AS rnk
    FROM 
        country_region cr
    INNER JOIN 
        country_national_dish cnd ON cr.Country_ID = cnd.Country_ID
    INNER JOIN 
        main_contents mc ON cnd.Dishes_ID = mc.Dishes_ID
    INNER JOIN 
        ingredients i ON mc.Ingredient_ID = i.Ingredient_ID
    GROUP BY 
        cr.Continent, i.Ingredient
),
TotalCounts AS (
    SELECT 
        Continent,
        SUM(Top_Ingredient_count) AS Total_Count
    FROM 
        IngredientCounts
    GROUP BY 
        Continent
)

SELECT 
    ic.Continent, 
    ic.Continent_Ingredient, 
    ic.Top_Ingredient_count,
    ROUND((ic.Top_Ingredient_count / tc.Total_Count) * 100, 2) AS Percent_Continent_Total
FROM 
    IngredientCounts ic
JOIN 
    TotalCounts tc ON ic.Continent = tc.Continent
WHERE 
    ic.rnk = 1
ORDER BY 
    ic.Continent, ic.Continent_Ingredient;
        
SQL Result
Continent Continent Ingredient Top Ingredient Count Percent Continent Total
Africa Vegetables 9 5.96
Asia Rice 19 16.81
Europe Potatoes 9 9.00
North America Rice 8 10.00
Oceania Coconut 5 9.26
South America Beef 6 12.24