在oracle中将具有相同值的多行组合成一行

时间:2018-03-22 04:52:22

标签: sql oracle

我一直在尝试解决Oracle SQL查询,但无济于事,并希望我能得到一些帮助。方案是我查询数据库并获得具有相同值的多个记录,并希望将多行合并为一个。我想要实现的是具有 IDNO = 22099575 in的客户记录一行而不是三行,因为它出现在我在下面的查询结果的附加屏幕截图中

SELECT concat(cu.firstname,cu.secondname) 
        Customername,cu.customerno,l.idnumber Idno,l.branch_code 
        Branchcode,l.phonenumber 
        Phone,cu.gender,l.grade,l.arocode,l.loanaccount,l.duedate,l.interest,
        l.outstandingamount Outstandingloanbal,
        l.lien Lienamount,TO_CHAR(l.applicationdate,'DD-MM-YY') 
        applicationdate,l.lastpaymentdate Lastcreditdate,l.inarrears 
        Principalloaninarrears,
        l.rebate_amount Rebatepayable, l.empcode, l.disbursaldate, lt.description 
        Producttype,sum(l.amountdisbursed) Disbursedamt,
        l.loanamount Principalamount,l.interest 
        Interestamount,l.flexi_refno,l.active
FROM ((ebank.tbloanaccount l
INNER JOIN ebank.tbcustomers cu ON l.customerno = cu.customerno)
INNER JOIN ebank.tbloantype lt ON l.productcode = lt.productcode) 
where l.DISBURSED = '1'
group by concat(cu.firstname,cu.secondname), cu.customerno, l.idnumber, 
        l.branch_code, l.phonenumber, 
        cu.gender, l.grade, l.arocode, l.loanaccount, 
        l.duedate, l.interest, l.outstandingamount, l.lien, 
        TO_CHAR(l.applicationdate,'DD-MM-YY'), 
        l.lastpaymentdate, l.inarrears, l.rebate_amount, l.empcode, l.disbursaldate, 
        lt.description, l.loanamount, l.interest, l.flexi_refno, l.active  order by 
        l.disbursaldate desc;

以下是上述查询结果的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在查询中使用dense_rank,获取最新的due_date,未偿还的贷款。

SELECT concat(cu.firstname,cu.secondname) 
    Customername,cu.customerno,l.idnumber Idno,l.branch_code 
    Branchcode,l.phonenumber 
    Phone,cu.gender,l.grade,l.arocode,l.loanaccount, max(l.duedate) keep ( dense_rank first order by l.duedate desc ) duedate,l.interest,
    max(l.outstandingamount) keep ( dense_rank first order by l.duedate desc ) Outstandingloanbal,
    l.lien Lienamount,TO_CHAR(l.applicationdate,'DD-MM-YY') 
    applicationdate,l.lastpaymentdate Lastcreditdate,l.inarrears 
    Principalloaninarrears,
    l.rebate_amount Rebatepayable, l.empcode, l.disbursaldate, lt.description 
    Producttype,sum(l.amountdisbursed) Disbursedamt,
    l.loanamount Principalamount,l.interest 
    Interestamount,l.flexi_refno,l.active 
FROM ((ebank.tbloanaccount l
INNER JOIN ebank.tbcustomers cu ON l.customerno = cu.customerno)
INNER JOIN ebank.tbloantype lt ON l.productcode = lt.productcode) 
where l.DISBURSED = '1'
group by concat(cu.firstname,cu.secondname), cu.customerno, l.idnumber, 
    l.branch_code, l.phonenumber, 
    cu.gender, l.grade, l.arocode, l.loanaccount, 
    l.duedate, l.interest, l.outstandingamount, l.lien, 
    TO_CHAR(l.applicationdate,'DD-MM-YY'), 
    l.lastpaymentdate, l.inarrears, l.rebate_amount, l.empcode, l.disbursaldate, 
    lt.description, l.loanamount, l.interest, l.flexi_refno, l.active  order by 
    l.disbursaldate desc;