我想知道在给定查询中没有 rownum 条件的情况下会返回多少行:
SELECT columns
, [number of rows before applying rownum] AS numberofrows
FROM tables
WHERE conditions
AND rownum < 100
是否可以不使用子查询?
COUNT()
您可以在嵌套查询中使用解析版本,例如:
SELECT * FROM
(
SELECT table_name,
COUNT(*) OVER() AS numberofrows
FROM all_tables
WHERE owner = 'SYS'
ORDER BY table_name
)
WHERE rownum < 10;
无论如何,您都需要嵌套它以在rownum
过滤器之前应用 order-by 以获得一致的结果,否则您会得到一组随机(ish)行。
您也可以rownum
用解析ROW_NUMBER()
函数替换:
SELECT table_name, cnt FROM
(
SELECT table_name,
COUNT(*) OVER () AS numberofrows,
ROW_NUMBER() OVER (ORDER BY table_name) AS rn
FROM all_tables
WHERE owner = 'SYS'
)
WHERE rn < 10;
在这种情况下,使用条件聚合:
select sum(case when conditions then 1 else 0 end) as NumWithConditions,
count(*) as Total
from tables;
您需要运行两个查询。首先找到集合的大小,然后找到数据。
select count(*) from tables where conditions;
然后是第二个。
请注意,在您订购您的套装之前,并将其包装到另一个选择
select * from (select columns from tables where conditions order by columns) rownum < 100
.