I have a SQL data base comparing different ratings that critics give to movies. I am trying to find the average of the critics' ratings for each unique movie and then create a new table to display these aggregated ratings.
I tried using the AVG(DISTINCT()) functionality, but it only found the averages by distinct ratings, not by distinct movies. Any help with aggregation would be huge!
ETA:
Sample data example
Movie
Rating
Jaws
1
Jaws
4
Jaws
3
HomeAlone
2
HomeAlone
5
Returned data example
Movie
Rating
Jaws
2.67
HomeAlone
3.5
So average all of the individual ratings by the movie name and then create a new table.
A possible solution is to group by Movie.
SELECT Movie, AVG(Rating) FROM [Table] GROUP BY Movie;
I think this will create the answer you want as the output in a new table
CREATE TABLE new_table AS
SELECT Movie, round(AVG(Rating),2)
FROM table_name
GROUP BY Movie;
Related
I have a simple query that I need to execute which will return all users who have watched the same movies, the date they did so and the number of times each movie was watched. The result should be grouped by movie title.
Say I have movie title: Gladiator and Prometheus. The result should be like:
Gladiator: Watched: 10 times| by: Alan, Bruce, Cecil, Marlon, etc| on: 10/01/2020, 1/11/2019, etc
Prometeus: Watched: 7 times| by: Julia, Baner, Alan, Marlon, etc | on: 10/01/2020, 1/11/2019, etc
I have MOVIE table, USER table, VIEW table
Some infos that might help you understand my problem:
MOVIE has view count row
VIEW has date row
USER has name, etc
My question is a bit similar to this: Query on all the people who has watched the same movies as I did but I wasn't able to adapt the solution to my case.
How can I achieve this?
I am assuming the following table structures:
movie: movie_name, view_count
user: user_name, age, city
view: movie_name, user_name, view_date
The query can be:
select m.movie_name as movie,
m.view_count as watched,
listagg(v.user_name, ',') as by,
listagg(v.view_date) as on
from movie m, view v
where m.movie_name = v.movie_name
group by m.movie_name, m.view_count;
Group by function listagg is available in Oracle's SQL. Microsoft's SQL has similar string_agg, in MySql group_concat etc.
So I have aggregation table (Players_Tournaments) of two others table: Players and Tournament. In aggregation table I save points for each player and each tournament. Tournaments have also relationship with table Season which defined in which Season was Tournament (etc. 2020, 2019...).
I want to delete rows from Player_Tournaments which are zeroes. But I want to delete only rows when they are grouped by Season in SUM gives zeroes. Becaues when I entering points for players in each tournament i get list of all my players and if they dont play they automaticly get 0 points. The main reason why I want to do this because many player didn't play all season and I want to delete those records.
I hope that was enough detailed.
EDIT:
Players_Tournaments: Player_id, Tournament_id, Points
Player: id, name...
Tournament: id, name, season_id...
Season: id, year
If you just want to remove the rows from a result set, you would use a HAVING clause:
HAVING SUM(points) <> 0
I have the two following schemes:
Movies[title, year, director, country, rating, genre, gross, producer]
and
Actors[title, year, characterName, actor]
Now I have the following exercise
Find character names that appeared in two movies produced in different countries.
My idea was the following which doesn't really work:
SELECT characterName
FROM Actors a
JOIN Movies m
ON a.title=m.title
AND a.year=m.year
WHERE COUNT(m.title)=2
AND COUNT(DISTINCT(m.country)=2
GROUP BY m.title;
My idea was to obviously select the characterName and join both tables on title and year because they are unique values in combination. Then my plan was to get the movies that are unique (by grouping them) and find the ones with a count of 2 since we want two movies. I hope that I am right till now.
Now I have my problems, because I don't really know how to evaluate if the movies played in two different locations.
I want to somehow make sure that they play in different countries.
You are on the right track. Here is a fixed version of your original query, that should get you the results that you expect:
select a.characterName
from actors a
inner join movies m
on m.title = a.title
and m.year = a.year
group by a.characterName
having count(distinct m.coutry) >= 2
Notes on your design:
it seems like you are using (title, year) as the primary key for the movies table. This does not look like a good design (what if two movies with the same title are produced the same year?). You would be better off with an identity column (in MySQL, an autoincremented primary key), that you would refer as a foreign key in the actors table
better yet, you would probably need to create a separate table to store the masterdata of the actors, and set up a junction table, say appearances, that represents which actors interpreted which character in which movie
I'm very new to SQL, so please bear with me.
I've built a movie database and I'm trying to query it so that all my tables display properly.
I have a movies table with the columns movieID, title, releaseYear, directorID, genreID, and actorID.
Inside the table director, I have directorID and Director.
Using the query SELECT * FROM movies INNER JOIN director ON director.directorID = movies.directorID;, I'm able to get everything in tables movies and director to display (which isn't exactly what I want, but it's in the right track).
My remaining tables are actor, (with actorID and actor's names) starring (with starringID, movieID, and actorID), genre (with genreID and 22 different genres), and moviegenres (with moviegenresID, moviesID, and genreID).
I'm a bit lost and I apologize if this is confusing and messy, but I'm thinking I need to query the database so that all the tables show the data and are associated with the correct column. For example, most movies have multiple genres and actors, which is why I separated them into tables of their own.
I can't figure out how to query everything to display properly in the result grid.
Thanks in advance
I am building a site one aspect of which involves teachers and rating teachers. Ratings are made by students, and when students are looking for teachers, among the criteria I'd like for them to be able to filter the teachers by include their average ratings.
Each rating by each student of each teacher puts the number of stars they rate the teacher into the database. I already have a query that can calculate the average stars for each teacher
SELECT AVG(star) AS Average FROM rating_table WHERE type = 'Member_review'
AND teacher_id = id_number
with id_number obviously being the user_id of the teacher.
When people are on the search page for all teachers, I'd love for them to be able to filter the teachers by rating, among other criteria. I can put this query in a foreach loop and have it run down each and every teacher id, but I suspect this will be a huge drag on the server - is there a way for me to put a string of comma-seperated ids into the query and have it find the average rating for each one? I did try this query and it did not work
SELECT AVG(star) AS Average FROM rating_table WHERE type = 'Member_review'
AND teacher_id IN(1,2,3, etc...)
Is there a way in which i can get the query to take the average for each teacher user_id and have it out put something like this?
Average Teacher ID
3.5 6
4.6 2
Yes. This is a basic aggregation query:
SELECT AVG(star) AS Average, teacher_id
FROM rating_table
WHERE type = 'Member_review' and
teacher_id IN(1,2,3, etc...)
group by teacher_id