现有一个需求,需要一SQL来实现其中逻辑,
具体如下
有两张表
1, Customer_info PK: Customer_ID

2. cust_balance_info PK: (Customer_ID,CONTEXTID,STATEMENTID,ACCOUNTID)

现要求查询结果如下:
查出customer 的Customer_ID , Customer_name , CONTEXTID , STATEMENTID, 和计算出来的loan 和Vehicles,,
其中
Loan逻辑定义为customer 的ACCOUNTID 为300对应的BALANCEVALUE 值
Vehicles 定义为
取此customer 的ACCOUNTID 为2300对应的BALANCEVALUE 值, 但是当ACCOUNTID 为2300对应的BALANCEVALUE值为0或者null 时,取该customer的ACCOUNTID 为2381对应的BALANCEVALUE 值,
如上例,其中ACCOUNTID为300或者2300/2381 可以hardcode
结果应该为

新年第一帖,求各位大神帮忙,谢谢。
------解决思路----------------------
select distinct
t1.cust_id,
t1.cust_nm,
t2.cont_id,
t2.stat_id,
(select min(bala_value) from cust_bala_info t3 where t3.cust_id=t2.cust_id and t3.cont_id=t2.cont_id and t3.stat_id=t2.stat_id and t3.acct_id=300) loan,
decode(nvl((select min(bala_value) from cust_bala_info t3 where t3.cust_id=t2.cust_id and t3.cont_id=t2.cont_id and t3.stat_id=t2.stat_id and t3.acct_id=2300),0)
,0
,(select min(bala_value) from cust_bala_info t3 where t3.cust_id=t2.cust_id and t3.cont_id=t2.cont_id and t3.stat_id=t2.stat_id and t3.acct_id=2381)
,(select min(bala_value) from cust_bala_info t3 where t3.cust_id=t2.cust_id and t3.cont_id=t2.cont_id and t3.stat_id=t2.stat_id and t3.acct_id=2300)) vehicles
from cust_info t1,cust_bala_info t2
where t1.cust_id=t2.cust_id;