0

我想知道在给定查询中没有 rownum 条件的情况下会返回多少行:

SELECT columns
, [number of rows before applying rownum] AS numberofrows
FROM tables
WHERE conditions
AND rownum < 100

是否可以不使用子查询?

4

3 回答 3

1

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;
于 2013-08-28T11:38:25.867 回答
0

在这种情况下,使用条件聚合:

      select sum(case when conditions then 1 else 0 end) as NumWithConditions,
             count(*) as Total
      from tables;
于 2013-08-28T11:41:09.293 回答
0

您需要运行两个查询。首先找到集合的大小,然后找到数据。

select count(*) from tables where conditions;

然后是第二个。

请注意,在您订购您的套装之前,并将其包装到另一个选择

select * from (select columns from tables where conditions order by columns) rownum < 100.

关于限制资源的讲座

于 2013-08-28T11:33:29.553 回答