1

甲骨文 11g

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

查询A

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

查询B

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
4

1 回答 1

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
于 2013-02-22T21:24:21.200 回答