根据利率和投资方式计算每月利息

时间:2018-10-16 05:39:52

标签: sql-server

我有一个包含investmentnameinterest_typerate_of_interestinvestment_datematurity_dateinvestment_amount

字段的sql表

在这里

  1. interest_type是指每月,每季度,每半年或每半年支付一次利息。
  2. rate_of_interest是指年利率
  3. investment_date是完成投资的日期
  4. maturity_date是到期日

我想每月获得所有投资的总利息。

我通过编写一个过程来做到这一点,在该过程中,我创建了一个表并按月转换上述数据,然后将其保存在那里,然后查询按月报告。

但是有什么办法可以通过编写sql查询来做到这一点?

参考数据

+----------------+---------------+------------------+-----------------+---------------+-------------------+
| investmentname | interest_type | rate_of_interest | investment_date | maturity_date | investment_amount |
+----------------+---------------+------------------+-----------------+---------------+-------------------+
| Invest1        | Monthly       | 5%               | 01/04/2018      | 30/10/2018    |              5000 |
| invest2        | Qtrly         | 7%               | 15/05/2018      | 14/05/2019    |             10000 |
| invest3        | yearly        | 10%              | 10/10/2018      | 09/10/2019    |             25000 |
+----------------+---------------+------------------+-----------------+---------------+-------------------+

输出

+----------------+-----------+---------+
| investmentname | int_month | amount  |
+----------------+-----------+---------+
| invest1        | Apr-18    |   20.83 |
| invest1        | May-18    |   20.83 |
| invest1        | Jun-18    |   20.83 |
| invest1        | Jul-18    |   20.83 |
| invest1        | Aug-18    |   20.83 |
| invest1        | Sep-18    |   20.83 |
| invest1        | Oct-18    |   20.83 |
| invest2        | Aug-18    |  175.00 |
| invest2        | Nov-18    |  175.00 |
| invest2        | Feb-19    |  175.00 |
| invest2        | May-19    |  175.00 |
| invest3        | Oct-19    | 2500.00 |
+----------------+-----------+---------+

2 个答案:

答案 0 :(得分:0)

在下面的查询中查找将为您提供预期的输出。

CREATE TABLE #test
(
    investmentname VARCHAR(50)
    ,interest_type CHAR(1)   --- Possible value M,Q,Y
    ,rate_of_interest int
    ,investment_date datetime
    ,maturity_date datetime
    ,investment_amount int
)


WITH CTE AS
(
    SELECT investmentname,interest_type,
              CASE WHEN interest_type='M' THEN    DATEADD(MONTH, 1, investment_date)
                    WHEN  interest_type='Q' THEN    DATEADD(QUARTER, 1, investment_date)
                    WHEN  interest_type='Y' THEN    DATEADD(YEAR, 1, investment_date)
                    ELSE investment_date 
            END  as investment_date from #test 

    UNION ALL
    SELECT C.investmentname,C.interest_type,
                CASE WHEN T.interest_type='M' THEN    DATEADD(MONTH, 1, C.investment_date)
                    WHEN  T.interest_type='Q' THEN    DATEADD(QUARTER, 1, C.investment_date)
                     WHEN  T.interest_type='Y' THEN    DATEADD(YEAR, 1, C.investment_date)
                    ELSE C.investment_date END 
    FROM CTE C
    INNER JOIN #test T ON T.investmentname=C.investmentname 
    WHERE 
    DATEADD(MONTH, 1, C.investment_date) <= T.maturity_date   

)

SELECT 
 T.investmentname
 , CONVERT(CHAR(4), T.investment_date, 100) + CONVERT(CHAR(4), T.investment_date, 120) 
 ,  CASE WHEN T.interest_type='M' THEN    InterestRatePerYear
                                 WHEN  T.interest_type='Q' THEN    InterestRatePerYear*3.0
                                 WHEN  T.interest_type='Y' THEN    InterestRatePerYear*12.0
                                 ELSE 0 END 
FROM CTE T
INNER JOIN 
(SELECT investmentname
        ,CAST(((investment_amount*rate_of_interest)/100.00)/12.0  AS NUMERIC(12,2))AS InterestRatePerYear
        FROM 
    #test )
Q ON T.investmentname=Q.investmentname
ORDER by investmentname

答案 1 :(得分:0)

请参阅以下演示:

DECLARE @t1 TABLE(
    investmentname nvarchar(MAX),
    interest_type nvarchar(MAX),
    rate_of_interest varchar(8),
    investment_date datetime2,
    maturity_date datetime2,
    investment_amount decimal (15,2)
);
INSERT @t1 VALUES
('Invest1', 'Monthly', '5%', '2018-04-01', '2018-10-30', 5000),
('Invest2', 'Qtrly', '7%', '2018-05-15', '2019-05-14', 10000),
('Invest3', 'Yearly', '10%', '2018-10-10', '2019-10-09', 25000);

WITH Split AS
(
    SELECT investmentname, interest_type, SUBSTRING(rate_of_interest, 1, LEN(rate_of_interest)-1)/100.0 rate_of_interest, investment_date, maturity_date, investment_amount, DATEADD(MONTH, DATEDIFF(MONTH, 0, investment_date), 0) D
    FROM @t1
    UNION ALL
    SELECT investmentname, interest_type, rate_of_interest, investment_date, maturity_date, investment_amount,
        CASE interest_type WHEN 'Monthly' THEN DATEADD(MONTH,1,D)
                           WHEN 'Qtrly' THEN DATEADD(MONTH,3,D)
                           WHEN 'Yearly' THEN DATEADD(YEAR,1,D)
        END
    FROM Split
    WHERE CASE interest_type WHEN 'Monthly' THEN DATEADD(MONTH,1,D)
                           WHEN 'Qtrly' THEN DATEADD(MONTH,3,D)
                           WHEN 'Yearly' THEN DATEADD(YEAR,1,D)
          END<=maturity_date
)
SELECT investmentname,
    CONVERT(varchar(3), D, 100)+'-'+CONVERT(varchar(2), D, 2) int_month,
    CAST(CASE interest_type WHEN 'Monthly' THEN investment_amount*rate_of_interest/12
                       WHEN 'Qtrly' THEN investment_amount*rate_of_interest/4
                       WHEN 'Yearly' THEN investment_amount*rate_of_interest
    END as decimal(15,2)) amount
FROM Split
WHERE D>investment_date
ORDER BY investmentname

结果

investmentname int_month amount
-------------- --------- --------
Invest1        May-18    20.83
Invest1        Jun-18    20.83
Invest1        Jul-18    20.83
Invest1        Aug-18    20.83
Invest1        Sep-18    20.83
Invest1        Oct-18    20.83
Invest2        Aug-18    175.00
Invest2        Nov-18    175.00
Invest2        Feb-19    175.00
Invest2        May-19    175.00
Invest3        Oct-19    2500.00

备注

缺少第一个月。如果应该计算第一个月的全额利息,则应该放宽where子句。