SELECT SUM 在没有记录时返回一行

2022-01-09 00:00:00 sql sum oracle

我发现一个查询存在一些问题,该查询从表中返回满足特定条件的所有记录的字段总和.当没有记录时,我希望收到未找到记录",但我收到的是空结果.

I'm finding some problems with a query that returns the sum of a field from a table for all the records that meet certain conditions. I expected to receive a "No records found' when there were no records, but instead I'm receiving a null result.

SQL> SELECT * FROM DUAL WHERE 1=2;

no rows selected
SQL> SELECT SUM(dummy) FROM DUAL WHERE 1=2;

SUM(DUMMY)
----------


SQL>

在这种情况下有什么办法不接收任何记录吗?

Is there any way to not receive any record in that case?

推荐答案

这个怎么样:

select my_sum
from
(SELECT SUM(dummy) as my_sum FROM DUAL WHERE 1=2)
where
my_sum is not null

相关文章