views:

33

answers:

1

So I need help modifying my query to not just include one movie from each city, but all movies from each individual city. Here is my professor's question: Find the average rating of all movies playing in each city. Display the city name and the computed rating. Order the results descending by rating.

Here is my definition for each table:

CREATE TABLE Theatres (
Name varchar2(50) not null,
City varchar2(50) not null,
State varchar2(50) not null,
Zip number not null,
Phone varchar2(50) not null,
PRIMARY KEY (Name)
);

CREATE TABLE Movies (
Title varchar2(100) not null,
Rating NUMBER not null,
Length NUMBER not null,
ReleaseDate date not null,
PRIMARY KEY (Title),
CHECK (Rating BETWEEN 0 AND 10),
CHECK (Length > 0),
CHECK (ReleaseDate > to_date('1/January/1900', 'DD/MONTH/YYYY'))
);

CREATE TABLE ShownAt (
TheatreName varchar2(50) not null,
MovieTitle varchar2(100) not null,
PRIMARY KEY (TheatreName, MovieTitle),
FOREIGN KEY (TheatreName) REFERENCES Theatres(Name),
FOREIGN KEY (MovieTitle) REFERENCES Movies(Title)
);

Here is the Select statement I have so far:

SELECT AVG(m.rating) AS avg_rating, 
     t.city
FROM THEATRES t
JOIN SHOWNAT sa ON sa.theatrename = t.name
JOIN MOVIES m ON m.title = sa.movietitle
GROUP BY t.city
ORDER BY avg_rating DESC

I feel bad - I completely do not understand this homework - I've asked almost every question on this site - but I learn by example, so everything I see here I usually then understand and can apply to other questions. Thanks everyone for their help!

A: 

Add movietitle to the select and group by clauses

Conrad Frix
that will change the avg to reflect the average rating for that movie, per city. There's nothing wrong with the query the OP provided.
OMG Ponies
Conrad thanks, that is exactly what I needed to do - here is what I changed it to:
Brian Lang
the OP's comnent said each movie, contrary to the original request.
Conrad Frix
So the answer doesn't work, but you're accepting it?
LittleBobbyTables
I feel a little dirty on this one
Conrad Frix
Perhaps I am misunderstanding the instructions of my professor (which I included word for word) but this answer seems to fit the bill - Am I reading it wrong and you see something different? Would love your input.
Brian Lang
Hah and so I looked at my responses, and to be honest, do not know WHY I put that the answer did not work, but would give you credit - I blame in on the Beer Fest here in Cincinnati - My Bad - Thank you Conrad and LittleBobby. My apologies for being a drunken idiot. That all being said, does everything match up accordingly in your eyes?
Brian Lang
Just to make sure, you don't want a list of each city with an average of all movies for that city, but instead you want an average of each movie for each city? Side note: that's funny, I was just at a craft beer tasting in Cincinnati a few weeks ago.
LittleBobbyTables
To be honest, I'm just going off the exact phrasing from my professor that I included in original question. And man, if you think this, or past weekend have been insane, next weekend is Octoberfest here - largest beer celebration in the United States - you should come. But otherwise yes, average of each movie playing in each city.
Brian Lang
@Brian: hope the head is feeling OK this morning...Since a given movie has only one rating, you can only get a single value as the 'average rating' for that movie - which is its rating. Now, the interesting question is "if a movie X is playing at N different theatres", does its rating get weighted by N or just by 1. But other than that, your original query (as in the answer) meets your professors requirements.
Jonathan Leffler