【问题标题】:return zero for no rows returned (in a complex query)对于没有返回的行返回零(在复杂查询中)
【发布时间】:2014-08-25 13:55:01
【问题描述】:

我在 oracle 中使用以下查询。如果没有返回行,我的查询的目的是返回零。否则我会收到“找不到数据”错误。我用谷歌搜索并在stackoverflow上遇到了coelese。我使用它如下:-

SELECT COALESCE( (select sum(transaction_detail.amount)
from
    lookup_state , lookup_city , lookup_bank ,transaction_detail
where
   transaction_detail.bank_id = lookup_bank.bank_id
and
   lookup_bank.city_id = lookup_city.city_id
and
   lookup_city.state_id = lookup_state.state_id
and 
   lookup_state.state_id = 3 
group by
   lookup_state.state_name, lookup_state.state_id) , 0) into state_total_amount ;

我收到以下错误:-

    from not present where expected

我做错了什么。提前致谢。

【问题讨论】:

  • 你错过了写外部SELECTStatement的FROM
  • 向我们展示输入和输出。你是想写一个 pl/sql 块还是只是一个查询?为我们提供初始脚本(例如,create table ... insert into t ...)或只是一个简单的查询,其中包含 with 部分(例如,with t as (...) select * from t...)。您将更快地获得帮助。

标签: sql oracle coalesce


【解决方案1】:

试试这个:

select coalesce(sum(transaction_detail.amount),0)
into state_total_amount
from
    lookup_state , lookup_city , lookup_bank ,transaction_detail
where
   transaction_detail.bank_id = lookup_bank.bank_id
and
   lookup_bank.city_id = lookup_city.city_id
and
   lookup_city.state_id = lookup_state.state_id
and 
   lookup_state.state_id = 3;

【讨论】:

    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 2011-11-17
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多