我正在尝试让数据库中的用户拥有数据库中最大段的所有权。为此,我正在尝试:
SELECT owner, MAX(bytes)
FROM SYS.DBA_SEGMENTS
GROUP BY owner
HAVING ROWNUM <= 1;
然而,这会返回"not a GROUP BY expression"
。为什么我不能只选择第一行?我该怎么写这个查询?谢谢!
答案 0 :(得分:5)
你可以。在Oracle 12c +中,您可以执行以下操作:
SELECT owner, MAX(bytes)
FROM SYS.DBA_SEGMENTS
GROUP BY owner
ORDER BY MAX(bytes) DESC
FETCH FIRST ROW ONLY;
请注意ORDER BY
。
在早期版本中,您需要一个子查询:
SELECT o.*
FROM (SELECT owner, MAX(bytes)
FROM SYS.DBA_SEGMENTS
GROUP BY owner
ORDER BY MAX(bytes) DESC
) o
WHERE rownum = 1;
答案 1 :(得分:1)
在早期版本中,您还可以使用(只需对数据进行一次传递):
select max(owner) keep (dense_rank last order by bytes nulls first) as owner,
max(bytes) as bytes
from sys.dba_segments;