Home > Back-end >  How could I find average of length each category In SQLite
How could I find average of length each category In SQLite

Time:11-08

I don't know why it's result like every avg is the same values I'm trying to solve this remove/add group by I still don't know why, please help me if I have to add or remove anything else

It's result like this

name avg(length)
Action 111
Animation 111

same avg values

I want result like this :

name avg(length)
Action 142
Animation 166

difference avg by their category

This is my statements

SELECT c.name,  AVG(f.length)
FROM film f, category c, film_category fc
WHERE fc.film_id = f.film_id
GROUP BY c.name

CodePudding user response:

You are not joining the category table. This results in a cross join which generates # of categories * # of film_categories records.

SELECT c.name, AVG(f.length)
FROM
   film f
   INNER JOIN film_category fc
       ON f.film_id = fc.film_id
   INNER JOIN category c
       ON fc.category_id = c.category_id
GROUP BY c.name
  • Related