I am trying to write a SQL Query which will count the number of ingredients associated with a recipe in addition to finding out the average rating for that recipe. I have the following tables involved in this query: recipes, recipeIngredients, and ratingList.
recipes holds recipeName
recipeIngredients is a transition table between an ingredients table and recipes recipeID identifies recipe and ingredientID identifies specified ingredient
ratingList is a list table w/ recipeID as a foreign key
The problem that I am having is that the below query counts the number of instances in the rating table. For instance if a recipe had 9 ratings and 2 ingredients my total for ingredientCount would be 18 (9*2). I am sure there is a way to do this (pretty sure it has to do w/ my INNER JOIN but I can't figure out a different way of getting there).
My goal is to select the recipeName, number of ingredients associated w/ that recipe and the avgRating for that recipe. Any help would be much apprecited!
SQL QUERY:
recipes holds recipeName
recipeIngredients is a transition table between an ingredients table and recipes recipeID identifies recipe and ingredientID identifies specified ingredient
ratingList is a list table w/ recipeID as a foreign key
The problem that I am having is that the below query counts the number of instances in the rating table. For instance if a recipe had 9 ratings and 2 ingredients my total for ingredientCount would be 18 (9*2). I am sure there is a way to do this (pretty sure it has to do w/ my INNER JOIN but I can't figure out a different way of getting there).
My goal is to select the recipeName, number of ingredients associated w/ that recipe and the avgRating for that recipe. Any help would be much apprecited!
SQL QUERY:
Code:
SELECT recipes.recipeName, Avg(ratingList.ratingValue) AS avgRatingValue,
recipeIngredients.recipeID, count(ingredientID) AS ingredientCount
FROM (recipes INNER JOIN ratingList ON recipes.recipeID=ratingList.recipeID) INNER JOIN recipeIngredients ON recipes.recipeID = recipeIngredients.recipeID
GROUP BY recipeIngredients.recipeID, recipes.recipeName