根据情况统计贷款周期

问题描述 投票:0回答:1

好的,我正在写一份报告来统计客户获得贷款的次数。 例如,如果客户只有一个贷款记录,我需要为他之前的贷款记录显示 1 和 null,因为这是他的第一个贷款记录。
但是如果客户获得了不止一条记录,例如,如果我将他所有的贷款记录相加,我会得到 4 条记录,而他之前的记录应该是 3

类似这样的事情

循环 p_周期
1 0
2 1
3 2
4 3

我已经尝试过了,效果很好,但仅适用于已经关闭之前贷款账户或没有并行当前贷款(同时> 2笔贷款)的客户 如果客户有超过 2 个并行的持续贷款,我的脚本会返回类似这样的内容

循环 p_周期
1 0
2 4
3 4
4 4
SELECT p_acct.cod_cust_id,
                         COUNT(p_acct.cod_cust_id) p_cycle
                    FROM acct_dtls c_acct,
                         acct_dtls p_acct
                   WHERE c_acct.cod_cust_id = p_acct.cod_cust_id
                     AND p_acct.dat_first_disb IS NOT NULL
                     AND c_acct.dat_first_disb IS NOT NULL
                     AND p_acct.dat_last_mnt =
                         (SELECT MAX(dd.dat_last_mnt)
                            FROM acct_dtls dd
                           WHERE dd.dat_first_disb IS NOT NULL
                             AND c_acct.cod_cust_id = dd.cod_cust_id
                             AND dd.dat_acct_open < c_acct.dat_acct_open)
                   GROUP BY p_acct.cod_cust_id

我在想是否有任何方法可以使用 union all 来实现我想要的结果或任何其他方式。

plsql psql plsqldeveloper
1个回答
0
投票

由于没有示例数据,我们所能做的就是尝试猜测数据结构的基本元素,并为您提供预期结果的一些指导。这是根据您的要求生成的两个虚拟表和 SQL 代码(带有一些额外的数据)。这里没有条件,但您可以过滤结果数据集或调整 Case 表达式以获得您想要的。

WITH    --  S a m p l e   D a t a : 
    acct (CUST_ID, CUST_ACCOUNT, CUST_NAME) AS 
        (   Select 101, '11111', 'John' From Dual Union All
            Select 102, '22222', 'Jane' From Dual Union All
            Select 103, '33333', 'Mike' From Dual Union All
            Select 104, '44444', 'Boby' From Dual
        ),
    acct_dtls (CUST_ID, LOAN_ID, AMOUNT, START_DATE, END_DATE) AS
        (   Select 101, 1011, 1000, To_Date('01.01.2021', 'dd.mm.yyyy'),  To_Date('31.12.2021', 'dd.mm.yyyy') From Dual Union All
            Select 102, 1021, 2000, To_Date('01.01.2020', 'dd.mm.yyyy'),  To_Date('31.12.2021', 'dd.mm.yyyy') From Dual Union All
            Select 102, 1022, 160, To_Date('01.01.2023', 'dd.mm.yyyy'),  To_Date('31.07.2023', 'dd.mm.yyyy') From Dual Union All
            Select 103, 1031, 3000, To_Date('01.01.2023', 'dd.mm.yyyy'),  To_Date('31.10.2023', 'dd.mm.yyyy') From Dual Union All
            Select 103, 1032, 2000, To_Date('01.06.2023', 'dd.mm.yyyy'),  Null From Dual Union All
            Select 103, 1033, 1000, To_Date('01.09.2023', 'dd.mm.yyyy'),  Null From Dual 
        )
--    M a i n   S Q L :
Select      a.CUST_ID, a.CUST_ACCOUNT, a.CUST_NAME,
            Nvl(Count(ad.LOAN_ID), 0) "TOT_LOANS",
            Case When Nvl(Count(ad.LOAN_ID), 0) = 0 Then 0 Else Nvl(Count(ad.LOAN_ID), 0) - 1 End "PREV_LOANS",
          --
            Min(ad.START_DATE) "FIRST_START_DATE", 
            Max(ad.END_DATE) "LAST_END_DATE",
            Count(Case When ad.START_DATE Is Not Null And ad.END_DATE Is Null Then 1 End) "PENDING_LOANS",
            Count(Case When ad.START_DATE Is Not Null And ad.END_DATE Is Not Null Then 1 End) "CLOSED_LOANS"
From            acct a
Left Join   acct_dtls ad ON(ad.CUST_ID = a.CUST_ID)
Group By    a.CUST_ID, a.CUST_ACCOUNT, a.CUST_NAME
Order By    a.CUST_ID, a.CUST_ACCOUNT, a.CUST_NAME

/*
   CUST_ID CUST_ACCOUNT CUST_NAME   TOT_LOANS PREV_LOANS FIRST_START_DATE LAST_END_DATE PENDING_LOANS CLOSED_LOANS
---------- ------------ ---------- ---------- --------- ----------------- ------------- ------------- ------------
       101        11111 John                1         0 01-JAN-21         31-DEC-21                 0            1
       102        22222 Jane                2         1 01-JAN-20         31-JUL-23                 0            2
       103        33333 Mike                3         2 01-JAN-23         31-OCT-23                 2            1
       104        44444 Boby                0         0                                             0            0  */
© www.soinside.com 2019 - 2024. All rights reserved.