views:

52

answers:

1

So I've been doing this all night - can't quite understand my homework, and sadly my professor is unavailable on the weekend. I've posted a few of these questions, this being the last one. I've got something to go on, but it needs working (and coming out of this I'd love to fully understand the answer so I don't need help on something similar again). Here it goes: Find the name and the phone number of the theaters that show the maximum number of movies. Make sure your query works when there is a tie between several theatres.

Here are my table declares (and thank you to EVERYONE helping me out tonight, I owe you big time).

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)
 );

I'm trying to apply some of the things I've learned from StackOverflow members help in other questions, but I'm not sure how to return something based on the max results of a column. Any help would be greatly appreciated.

+1  A: 

Here's one way.

With T As
(
SELECT T.Name,  T.Phone, 
RANK() OVER (ORDER BY COUNT(S.MovieTitle ) DESC) AS Rnk
FROM Theatres T
JOIN ShownAt  S ON S.TheatreName= T.Name 
GROUP BY T.Name,  T.Phone
) 
SELECT Name, Phone
FROM T
WHERE Rnk=1;
Martin Smith