我有这个问题 - 我在oracle中的查询是贷款的抵押品和贷款的保险 - 代码就是这个 -
select dc.comm_file loan_id, -- loan ID
--------Collaterals part
dc.coll_id, --collateral ID
----------Insurance part
li.pol_number ins_id
from debts_collaterals dc
left join loan_insurances li on substr(dc.comm_file, 1, 6) = li.event and dc.branch = li.branch and li.date_key = to_date(:date, 'yyyymmdd')
and substr(dc.comm_file, 7, 2) = li.amendm --some loans must not have insurance
where dc.date_key = to_date(:date, 'yyyymmdd')
and dc.product in ('401','402','405')
and dc.coll_status != 'L'
and dc.comm_status = 'Y'
and li.status = 'E'
and dc.comm_file = '01424401'
结果是:
loan_id coll_id insurance_id
1424401 1320 00000000000000755500
1424401 1320 00000000000000755501
1424401 1321 00000000000000755500
1424401 1321 00000000000000755501
我的问题是如何在结果中显示2行 - 而不是4行 - 等等。我希望我的结果是 -
loan_id coll_id insurance_id
1424401 1320 00000000000000755500
1424401 1321 00000000000000755501
逻辑是 - 1个人可以有2个或更多的贷款抵押品,但0保险(在这种情况下,列ins_id值将为NULL)或类似于示例2抵押品和2个保险 - 等ins_id仅连接到loan_id - ins_id和coll_id之间没有连接。例如,如果1个人有3个抵押贷款和2个保险 - 结果应该是3行(因为贷款有3个抵押品)而在第3行ins_id将是NULL,因为贷款只有2个保险。