SQL分为两列

时间:2015-02-15 19:59:01

标签: oracle

让以下影院数据库,用户可以评论一部电影并为其评论家分配0到10分:

MOVIE (idMovie)
  PK (idMovie)
CRITICS (idMovie, user, score)
  PK (idMovie, user)
  FK (idMovie) REF MOVIE
  NN score

如何显示平均评分超过80%的所有电影ID?类似的东西:

WITH CMINUSEIGHT AS (
  SELECT C.idFilm, COUNT(C.idFilm) AS cpCount FROM CRITIQUE C
    WHERE C.note > 8
    GROUP BY C.idFilm),
  AVERAGE AS (
  SELECT C.idFilm, (COUNT(C.idFilm) / CP.cpCount * 100) AS cmCount FROM CRITIQUE C, CPLUSHUIT CP
    WHERE C.note < 9
    GROUP BY C.idFilm)
SELECT C.idFilm FROM CRITIQUE C, AVERAGE A
  WHERE C.idFilm = A.idFilm
  HAVING AVERAGE > 80;

感谢。

1 个答案:

答案 0 :(得分:1)

使用AVG聚合功能来完成此任务:

SELECT 
  idMovie, 
  AVG(score) AS AvgScore
FROM Critiques
GROUP BY idMovie
HAVING AVG(score) > 8