SELECT UDAF(AGGR())中的怪异行为

时间:2018-08-27 11:32:47

标签: sql oracle oracle11g oracle10g

有两个选择语句:

select       max(min(str)) from (select 0 id, 'a' str from dual) group by id having min(str) = 'a';
select strconcat(min(str)) from (select 0 id, 'a' str from dual) group by id having min(str) = 'a';

唯一的区别是外层聚合函数:max()strconcat()
您可以将strconcat()替换为您拥有的任何UDAF。

前一条语句按预期工作:返回字符串'a'
后一种说法:
 -(在Oracle 10g上)给出错误的结果(空而不是字符串'a'
 -(在Oracle 11g上)提高ORA-00979: not a GROUP BY expression

我不明白此错误消息。
您能解释一下这种行为吗?
这是一个Oracle错误吗?

1 个答案:

答案 0 :(得分:1)

10g:

似乎WM_CONCAT对我可用(是的,没有记录,但在这种情况下没有关系)或您使用的STRCONCAT(或其他一些功能)需要设置为“上”;看到这个例子:

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> select       max(min(str)) result
  2  from (select 0 id, 'a' str from dual)
  3  group by id having min(str) = 'a';

R
-
a

SQL> -- returns NULL, just as you've said
SQL> select wm_concat(min(str)) result
  2  from (select 0 id, 'a' str from dual)
  3  group by id having min(str) = 'a';

RESULT
---------------------------------------------------------------------


SQL> -- but, if we put it a level "up", the result is OK
SQL> select wm_concat(minstr) result
  2  from (select min(str) minstr
  3        from (select 0 id, 'a' str from dual)
  4        group by id having min(str) = 'a'
  5       );

RESULT
---------------------------------------------------------------------
a

SQL>

11g:

好的,两个查询(我在这里使用listagg

SQL> select       max(min(str)) result
  2  from (select 0 id, 'a' str from dual)
  3  group by id having min(str) = 'a';

R
-
a

SQL>
SQL> select listagg(min(str), ',') within group (order by null) result
  2  from (select 0 id, 'a' str from dual)
  3  group by id having min(str) = 'a';

RESULT
-----------------------------------------------------------------------
a

SQL>

那么,这是一个错误吗?我不知道。