简体   繁体   English

NVL中的Oracle Subselect(按要求分组)

[英]Oracle Subselect in NVL (Group By required)

I'm facing a problem in Oracle. 我在Oracle中面临一个问题。 I had a SQL where some values were fixed. 我有一个SQL,其中某些值是固定的。 Now I started replacing them with values from a parameter-table. 现在,我开始用参数表中的值替换它们。 Some of these fixed values where in a NVL(). NVL()中的某些固定值。

Simply said my statement is like this. 简单地说,我的陈述就是这样。

SELECT NVL(MAX(t.datefield), to_date('01011900','DDMMYYYY'))
FROM table t;

That works fine. 很好

Now I want to replace the fixed date to a date from my parameter-table with a subselect, which doesn't work at all. 现在,我想用子选择将固定日期替换为参数表中的日期,这根本不起作用。

// Works
SELECT NVL(MAX(NULL), 'hello') FROM DUAL;

// Doesn't work
SELECT NVL(MAX(NULL), (SELECT 'hello' FROM DUAL)) FROM DUAL;

The error is: 错误是:

ORA-00937: .... "not a single-group group function"

I have no idea how to group by a subselect. 我不知道如何按子选择分组。

Any help is very appreciated! 任何帮助都非常感谢! Thanks! 谢谢!

You can't group by a sub-select. 您无法通过子选择进行分组。 However, in order to achieve this your sub-select is only going to be able to return one row. 但是,为了实现这一点,您的子选择只能返回一行。 So, change it into a Cartesian join and group by that. 因此,将其更改为笛卡尔连接并以此分组。

SELECT NVL(MAX(NULL), str) 
  FROM DUAL
 CROSS JOIN ( SELECT 'hello' as str FROM DUAL )
 GROUP BY STR

More generally every column that is not included in an aggregate function must be included in the GROUP BY. 更一般而言,聚合函数中未包含的每个列都必须包含在GROUP BY中。 Plus NVL() is bad ; 再加上NVL()不好 use COALESCE() or CASE instead. 使用COALESCE()CASE代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM