生成账龄报告

nimxete2  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(409)

我需要生成一个账龄报告使用下面的样本数据从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)支付金额和零支付日期表示尚未支付摊销本金。下面是我对特定老化日期的期望输出。
账龄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 |
    +---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+

它似乎重复了三个摊销表(3月、4月、5月)上的到期、付款和余额的价值
希望得到积极的回应。蒂亚!

cig3rfwq

cig3rfwq1#

您的查询只提供一次正确的结果 group by 以及 sum 用过的。我错过什么了吗?

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

请检查这里的小提琴。

相关问题