是否允许在having子句中使用over()?

时间:2018-01-26 13:23:33

标签: sql oracle

我需要在over(partition by ...)子句中使用having

这是我的代码,它给出了错误:

  

群组功能嵌套太深

select resort, discipline, gender, count(1)
from races
group by resort, discipline, gender
having sum(count(1)) over (partition by resort, discipline) > 10;

有没有办法在over()条款中使用having,还是有替代方法?

2 个答案:

答案 0 :(得分:2)

您可以尝试选择子集。

select resort, discipline, gender from 
(select resort, discipline, gender, sum(count(1)) over (partition by resort, discipline) groupcount
from races
group by resort, discipline, gender) Records
where groupcount > 10

答案 1 :(得分:1)

你总是可以嵌套它:

WITH cte AS (
  select resort, discipline, gender, count(1) AS cnt
  from races
  group by resort, discipline, gender
), cte2 AS (
  SELECT *, sum(cnt) over (partition by resort, discipline) AS s
  FROM cte
)
SELECT *
FROM cte2
WHERE s > 10;