簡體   English   中英

生成老化報告

[英]Generating Aging Report

我需要使用來自tblAccount的以下示例數據生成老化報告。

+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+
| Loan ID | Account  |     Name      | Amortization Date | Amortized Principal | Paid Amount | Date Paid | Balance |
+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+
|       2 | A0007787 | JIMMY NEUTRON | 3/9/2020          |             3823.53 |     3823.53 | 3/9/2020  |       0 |
|       2 | A0007787 | JIMMY NEUTRON | 4/9/2020          |             3823.53 |         500 | 3/9/2020  | 3323.53 |
|       2 | A0007787 | JIMMY NEUTRON | 5/9/2020          |             3823.53 |           0 | NULL      | 3823.53 |
+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+

零 (0) 支付金額和NULL支付日期表示尚未支付攤銷本金。 下面是我想要的特定老化日期的 output。

帳齡 A - 3 月 12 日帳齡已於 3 月 12 日支付 3 月 9 日的時間表

+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-----
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+
| Loan ID | Account  |     Name      |    Due     |  Payment   | Balance | Current |  30   |  60   |  90   | 120 and Above |
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+
|       2 | A0007787 | JIMMY NEUTRON |  3,823.53  |  3,823.53  |       0 |    -    |  -    |  -    |  -    |          -    |
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+

老化 B - 5 月 10 日500.00 已於 4 月 9 日支付 4 月 9 日的時間表

+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+
| Loan ID | Account  |     Name      |    Due     | Payment  |  Balance   |  Current   |  30   |  60   |  90   |  120 and Above  |
+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+
|       2 | A0007787 | JIMMY NEUTRON |  3,823.53  |  500.00  |  3,323.53  |  3,323.53  |  -    |  -    |  -    |            -    |
+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+

時效 C - 6 月 10日 4 月 9 日計划有剩余余額,5 月 9 日沒有付款

時間表

    +---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+
| Loan ID | Account  |     Name      |    Due     | Payment |  Balance   |  Current  |     30     |     60     |  90   |  120 and Above  |
+---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+
|       2 | A0007787 | JIMMY NEUTRON |  7,147.06  |     500 |  7,147.06  |      -    |  3,823.53  |  3,323.53  |  -    |            -    |
+---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+

我在這里查看並嘗試了建議的解決方案,制作了 tweeks,但它似乎不適合我想要制作的內容。

目前,我有這種存儲過程:

INSERT INTO @Aging (
Loan, ID, Account, Name, AmortizationSchedule,  AmortizedPrincipal, PaidAmount, DatePaid, Balance)
SELECT Loan, ID, Account, Name, AmortizationSchedule,   AmortizedPrincipal, PaidAmount, DatePaid, Balance
 FROM tblAccount

Select DISTINCT Loan, ID, Account, Name, AmortizationSchedule,  AmortizedPrincipal, PaidAmount, DatePaid, Balance
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) < 1 then balance else 0 end) as [Current],
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) between 1 and 30 then balance else 0 end) as [DueTo30],
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) between 31 and 60 then balance else 0 end) as [DueTo60],
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) between 61 and 90 then balance else 0 end) as [DueTo90],
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) between 91 and 120 then balance else 0 end) as [DueTo120],
    (case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf  )) > 120 then balance else 0 end) as [Over120]
from @Aging where balance<>0.00  
order by Account

但它給出了以下結果,

 +---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+
    | Loan ID | Account  |     Name      |     Due     |  Payment   |   Balance   |  Current  |  30   |     60      | 90 |  120 and Above  |
    +---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+
    |       2 | A0007787 | JIMMY NEUTRON |  90,000.00  |  8,647.06  |  14,294.12  |      -    |  -    |  14,294.12  |  0 |               0 |
    +---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+

似乎它在攤銷計划的數量上重復了到期、付款和余額的價值,即三個(三月、四月、五月)

希望得到積極的回應。 蒂亞!

一旦使用group bysum ,您的查詢就會給出正確的結果。 我錯過了什么嗎?

Select DISTINCT [Loan ID], Account, [Name]
,sum(AmortizedPrincipal) Due, sum(PaidAmount) Payment, sum(Balance) Balance,
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) < 1 then balance else 0 end) as [Current],
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) between 1 and 30 then balance else 0 end) as [DueTo30],
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) between 31 and 60 then balance else 0 end) as [DueTo60],
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) between 61 and 90 then balance else 0 end) as [DueTo90],
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) between 91 and 120 then balance else 0 end) as [DueTo120],
    sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,@DateAsOf)) > 120 then balance else 0 end) as [Over120]
from @Aging where balance<>0.00  
GROUP BY [Loan ID], Account, [Name]
order by Account

請在此處檢查 db<>fiddle。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM