2013-01-12 54 views
0

獲取收視率最高的電影,我有三個表:SQL - 按流派

CREATE TABLE Movie 
    (
     movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, 
     title VARCHAR(255) NOT NULL, 
     moviePath VARCHAR(500) NOT NULL 
    ); 

    CREATE TABLE Rating 
    (
     rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, 
     mid INTEGER FOREIGN KEY REFERENCES Movie(movieId) ON DELETE CASCADE, 
     uid INTEGER FOREIGN KEY REFERENCES User(id) ON DELETE CASCADE, 
     rating INTEGER NOT NULL, 
    ); 

    CREATE TABLE Genre(
     id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, 
     movieId INTEGER NOT NULL FOREIGN KEY REFERENCES Movie(movieId) ON DELETE CASCADE, 
     genre VARCHAR(255) NOT NULL 
    ); 

我想創建從最多見於類型的SQL查詢,給了我最多見於電影(與moviepath,標題)回。

任何想法?

UPDATE

結果:

| MID | TITLE | MOVIEPATH | 
-------------------------------- 
| 4 | Happy days |   a | 
| 4 | Happy days |   a | 
+3

我想'movieId'不應該在'Genre'表,但一個'GenreId'應該是'Movie'表。而你的表格對於這樣的查詢是不完整的,你需要一些額外的細節。 –

+0

你是什麼意思? – maximus

+0

與您當前的表中每個類型可以有一個電影,但與我告訴每個電影有一個流派的表格。 –

回答

1

您可以通過在子查詢中計算電影的所有評分來計算評分。另一個子查詢可以計算每種類型的最高評分。通過將其連在一起,你會每流派過濾只有頂級的電影:

select * 
from Movie m 
join Genre g 
on  g.movieId = m.movieId 
join (
     select r.mid 
     ,  sum(Rating) as SumRating 
     from Rating r 
     group by 
       r.mid 
     ) r 
on  r.mid = m.movieId 
join (
     select g.id as gid 
     ,  max(SumRating) as MaxGenreRating 
     from (
       select r.mid 
       ,  sum(Rating) as SumRating 
       from Rating r 
       group by 
         r.mid 
       ) r 
     join Genre g 
     on  g.movieId = r.mid 
     group by 
       g.id 
     ) filter 
on  filter.gid = g.id 
     and filter.MaxGenreRating = r.SumRating 
0

我們可以在哪裏找到的看法計數?有了這個表,你可以使用這樣的查詢發現收視率最高的電影:

select Movie.movieId, Movie.title, Movie.moviepath 
from movie, rating, genre 
where 
    movie.id = rating.mid and 
    movie.id = genre.movieId 
order by rating desc 
limit 1; // limit is used if you are using MySQL. In other databases you can use suitable sql query. 

但是,如果你正在尋找從最看出流派最多見於電影,你必須有每部電影的觀看次數和表格中的每種類型。

0

我可以使用的等級合計最大連同組的流派建議您

select max(Rating.rating) as max_rating, Genre.genre, Movie.movieId from Movie inner join Rating on Movie.movieId = Rating.mid inner join Genre on Movie.movieId = Genre.movieId group by Genre.genre;

我不太確定,因爲我沒有嘗試它,所以這將工作100%,但我的想法是使用group by。它適用於count,max,min,avg等聚合。

我希望幫助

2

這真是太棒了,如果你已經提供了一些樣本數據來匹配..那麼嘗試了這一點。看看你以前的問題,這個答案就是草擬的。

SQLFIDDLE DEMO

select t.mid, t.sum_rating, 
m.title, m.moviepath, g.genres 
from (
    select mid, 
     sum(rating) as sum_rating, 
     dense_rank() over (order by 
     sum(rating) desc) as rnk 
    from rating 
    group by mid 
) t 
left join movie m 
on m.movieid = t.mid 
left join genre g 
on g.movieid = m.movieid 
where t.rnk = 1; 

結果:

| MID | SUM_RATING |  TITLE | MOVIEPATH | GENRES | 
------------------------------------------------------ 
| 4 |   37 | Happy days |   a | comedy | 
| 4 |   37 | Happy days |   a | RomCom | 

你可以使用這個選擇,因爲HSQL不支持dense_rank

查詢有:order by desc and top 1

-- alternatively 

select t.mid, t.sum_rating, 
m.title, m.moviepath, g.genres 
from (
    select top 1 mid, 
     sum(rating) as sum_rating 
    from rating 
    group by mid 
    order by sum_rating desc 
) t 
left join movie m 
on m.movieid = t.mid 
left join genre g 
on g.movieid = m.movieid 
; 
+0

hsql不支持dense_rank()。有沒有可能的解決方案沒有dense_rank? – maximus

+0

@maximus是你需要的)我的意思是預期的結果)?請澄清,注意我使用過的樣本數據,並且如果您需要對樣本數據和表格結構進行任何更改,請發表評論。如果是這樣,我們可以得到與HSQL其他手段相同的.. – bonCodigo

+0

我只需要mid,title,moviepath – maximus