ORA-30483:这里不允许使用窗口函数?

时间:2017-11-16 04:47:16

标签: sql oracle average having

我需要显示具有特定平均评分的电影列表。我知道问题出在HAVING子句中,但我不确定如何修复它。如果没有该子句,它就会起作用,它会列出所有遵循指定质量的电影/演员组合,但我需要它们的平均评分也高于4(这是我尝试使用having子句)。

使用HAVING子句,它表示ORA-30483:此处不允许使用窗口函数。有谁知道如何解决这个问题?

SELECT DISTINCT m.title "Movie Title",
   ROUND(AVG(r.rating) OVER (PARTITION BY m.movieid, a.actorid),2) "Average Rating",
   a.afname||' '||a.alname "Name",
   m.ryear
FROM netflix.actors a JOIN netflix.movies_actors ma
   ON a.actorid = ma.actorid
JOIN netflix.movies m
   ON ma.movieid = m.movieid
JOIN netflix.ratings100 r
   ON m.movieid = r.movieid
JOIN netflix.movies_genres g
   ON m.movieid = g.movieid
WHERE a.gender LIKE 'F'
   AND g.genrecode LIKE 'DRM'
   AND m.ryear >= TO_CHAR(SYSDATE, 'yyyy')-12
HAVING AVG(r.rating) OVER (PARTITION BY m.movieid, a.actorid) >= 4
ORDER BY "Average Rating" desc

2 个答案:

答案 0 :(得分:0)

仅在select语句中允许使用窗口或分析函数。 所以你可以像这样过滤:

    select * from (
    SELECT DISTINCT m.title "Movie Title",
       ROUND(AVG(r.rating) OVER (PARTITION BY m.movieid, a.actorid),2) "Average Rating",
       a.afname||' '||a.alname "Name",
       m.ryear
    FROM netflix.actors a JOIN netflix.movies_actors ma
       ON a.actorid = ma.actorid
    JOIN netflix.movies m
       ON ma.movieid = m.movieid
    JOIN netflix.ratings100 r
       ON m.movieid = r.movieid
    JOIN netflix.movies_genres g
       ON m.movieid = g.movieid
    WHERE a.gender LIKE 'F'
       AND g.genrecode LIKE 'DRM'
       AND m.ryear >= TO_CHAR(SYSDATE, 'yyyy')-12


    ) as f1
    where f1.[Average Rating] >=4
ORDER BY f1.[Average Rating] desc

答案 1 :(得分:0)

Google Analytics功能无法在WHEREHAVING bloks中使用。他们在SELECT - 块中计算结果集。而且,如果没有HAVING,您也会使用GROUP BY,这也是一个错误。 我认为您可以使用子查询和WHERE

SELECT *
FROM
  (
    SELECT DISTINCT m.title "Movie Title",
       ROUND(AVG(r.rating) OVER (PARTITION BY m.movieid, a.actorid),2) "Average Rating",
       a.afname||' '||a.alname "Name",
       m.ryear
    FROM netflix.actors a JOIN netflix.movies_actors ma
       ON a.actorid = ma.actorid
    JOIN netflix.movies m
       ON ma.movieid = m.movieid
    JOIN netflix.ratings100 r
       ON m.movieid = r.movieid
    JOIN netflix.movies_genres g
       ON m.movieid = g.movieid
    WHERE a.gender LIKE 'F'
       AND g.genrecode LIKE 'DRM'
       AND m.ryear >= TO_CHAR(SYSDATE, 'yyyy')-12
  )
WHERE "Average Rating" >= 4
ORDER BY "Average Rating" desc