没有返回任何行返回零(在复杂查询中)

时间:2014-07-04 09:11:04

标签: sql oracle coalesce

我在oracle中使用以下查询。如果没有返回任何行,我的查询的目的是返回零。否则我得到'没有找到数据'错误。我用Google搜索并在stackoverflow上遇到了coelese.i使用它如下: -

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

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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;