如何包含作为列值返回的总行数?

时间:2013-02-22 21:20:23

标签: sql oracle11g max rownum

Oracle 11g

我想将max(rownum)作为新列包含以获得所需的结果。本质上,我想将此 QueryA 的结果作为新列添加到 QueryB。当我在QueryB中包含max(rownum)时,我得到一个'不是一个组功能。当我按可乐或rownum分组时,QueryB也不起作用。

QueryA

with data_row as
(
select 1 as col_a from dual union all
select 2 as col_a from dual union all 
select 3 as col_a from dual )
select max(rownum) as max_row from data_row

QueryB

with data_row as
( select 1 as col_a from dual union all
  select 2 as col_a from dual union all
  select 3 as col_a from dual)
select col_a, rownum from data_row

期望的结果

Col_a    Rownum   MaxRowNum
--------------------------------
1          1       3
2          2       3
3          3       3

1 个答案:

答案 0 :(得分:2)

您可以使用count函数的分析表单

SQL> ed
Wrote file afiedt.buf

  1  with data_row as (
  2    select 1 as colA from dual union all
  3    select 2 as cola from dual union all
  4    select 3 as cola from dual
  5  )
  6  select colA, rownum, count(*) over () cnt
  7*   from data_row
SQL> /

      COLA     ROWNUM        CNT
---------- ---------- ----------
         1          1          3
         2          2          3
         3          3          3