基于条件的利息计算

时间:2017-01-16 02:22:32

标签: sql sql-server sql-server-2008

我需要一个关于sql server计算利息的建议。下面是一个月内Mr.x的帐户交易详情

期初余额:100

 DATE     Deposit  W/d  BALANCE
1         10000          10100
2                        10100
3                        10100
4                        10100
5         40000          50100
6                        50100
7                        50100
8                        50100
9                 45000   5100

下面是Mr. Jan的余额详情

打开Bal = 100

7月1日=卢比。 10100

7月5日= 50,100卢比

7月9日= 5000卢比(在他撤回45,000卢比之后)

现在我需要在月末将一些表格下面的信息插入

FromDate    Todate  Noofdays    Balance
01.01.2017  04.01.2017  4        10100
05.01.2017  08.01.2017  4        50100
09.01.2017  31.01.2017  23        5000

请任何人帮助我获取sql server

中的天数和余额详情

2 个答案:

答案 0 :(得分:2)

考虑到您的Date列存储在DATE类型

这是一种方式

WITH data
     AS (SELECT *,
                Row_number()OVER(ORDER BY date) - 
                Row_number()OVER(partition BY BALANCE ORDER BY date) AS grp
         FROM   yourtable)
SELECT Min(date),
       Max(date),
       Datediff(dd, Min(date), Max(date)) + 1,
       BALANCE
FROM   data
GROUP  BY grp,
          BALANCE 

答案 1 :(得分:1)

我没有使用余额栏,脚本会自动每天计算余额

“交叉申请(价值(100,12017))AS i(opensbalance,mon,yr)” 是输入参数。

    CREATE TABLE #tt(D datetime,Deposit MONEY,[W/d] money,BALANCE MONEY)
    INSERT INTO #tt
    SELECT '01/01/2017',10000,NULL,10100 UNION
    SELECT '01/02/2017',NULL,NULL,10100 UNION
    SELECT '01/03/2017',NULL,NULL,10100 UNION
    SELECT '01/04/2017',NULL,NULL,10100 UNION
    SELECT '01/05/2017',40000,NULL,50100 UNION
    SELECT '01/06/2017',NULL,NULL,50100 UNION
    SELECT '01/07/2017',NULL,NULL,50100 UNION
    SELECT '01/08/2017',NULL,NULL,50100 UNION
    SELECT '01/09/2017',NULL,45000,5100
    SELECT * FROM #tt
            SELECT CONVERT(DATE,MIN(t.d)) AS FromDate,CONVERT(DATE,MAX(t.d)) AS Todate,t.BALANCE,DATEDIFF(d,MIN(t.d),MAX(t.d))+1  AS Noofdays
    FROM (
        SELECT d.d,a.Deposit,a.[W/d] 
                ,i.openingbalance+b.balance AS BALANCE
        FROM master.dbo.spt_values AS sv 
        CROSS APPLY(VALUES(100,1,2017)) AS i(openingbalance,mon,yr)
        CROSS APPLY(VALUES(DATEADD(DAY,sv.number,DATEADD(MONTH,i.mon-1,DATEADD(YEAR,i.yr-1900,0))))) d(d)
        LEFT JOIN #tt AS a ON DATEDIFF(d,a.d,d.d)=0
        OUTER APPLY(
                SELECT SUM(ISNULL(tt.Deposit,0))-SUM(ISNULL(tt.[W/d],0)) AS balance 
                FROM #tt AS tt WHERE DATEDIFF(d,tt.d,d.d)>=0
        ) b
        WHERE sv.type='P' AND sv.number BETWEEN 0 AND 30
        AND MONTH(d.d)=i.mon
        -- ORDER BY  d.d
    ) AS t
    GROUP BY t.BALANCE
    ORDER BY MIN(t.d)
FromDate   Todate     BALANCE               Noofdays
---------- ---------- --------------------- -----------
2017-01-01 2017-01-04 10100.00              4
2017-01-05 2017-01-08 50100.00              4
2017-01-09 2017-01-31 5100.00               23

- 版本2,客户ID为--------

CREATE TABLE #tt(CustID INT,D datetime,Deposit MONEY,[W/d] money,BALANCE MONEY)
INSERT INTO #tt
SELECT 1,'01/01/2017',10000,NULL,10100 UNION
SELECT 1,'01/02/2017',NULL,NULL,10100 UNION
SELECT 1,'01/03/2017',NULL,NULL,10100 UNION
SELECT 1,'01/04/2017',NULL,NULL,10100 UNION
SELECT 1,'01/05/2017',40000,NULL,50100 UNION
SELECT 1,'01/06/2017',NULL,NULL,50100 UNION
SELECT 1,'01/07/2017',NULL,NULL,50100 UNION
SELECT 1,'01/08/2017',NULL,NULL,50100 UNION
SELECT 1,'01/09/2017',NULL,45000,5100 UNION
SELECT 2,'01/06/2017',1000,NULL,NULL UNION
SELECT 2,'01/07/2017',2000,NULL,NULL UNION
SELECT 2,'01/08/2017',5000,NULL,NULL UNION
SELECT 2,'01/09/2017',NULL,4000,NULL

--   SELECT * FROM #tt
SELECT t.CustID, CONVERT(DATE,MIN(t.d)) AS FromDate,CONVERT(DATE,MAX(t.d)) AS Todate,t.BALANCE,DATEDIFF(d,MIN(t.d),MAX(t.d))+1  AS Noofdays
FROM (
    SELECT  i.CustID, d.d,a.Deposit,a.[W/d] 
            ,i.openingbalance+isnull(b.balance,0) AS BALANCE
    FROM master.dbo.spt_values AS sv 
    CROSS APPLY(VALUES(1,100,1,2017),(2,500,1,2017)) AS i(CustID,openingbalance,mon,yr)
    CROSS APPLY(VALUES(DATEADD(DAY,sv.number,DATEADD(MONTH,i.mon-1,DATEADD(YEAR,i.yr-1900,0))))) d(d)
    LEFT JOIN #tt AS a ON DATEDIFF(d,a.d,d.d)=0 AND a.CustID=i.CustID
    OUTER APPLY(
            SELECT SUM(ISNULL(tt.Deposit,0))-SUM(ISNULL(tt.[W/d],0)) AS balance 
            FROM #tt AS tt WHERE DATEDIFF(d,tt.d,d.d)>=0 AND tt.CustID=i.CustID

    ) b
    WHERE sv.type='P' AND sv.number BETWEEN 0 AND 30
    AND MONTH(d.d)=i.mon
   -- ORDER BY i.CustID,d.d
) AS t
GROUP BY t.CustID, t.BALANCE
ORDER BY t.CustID, MIN(t.d)
CustID      FromDate   Todate     BALANCE               Noofdays
----------- ---------- ---------- --------------------- -----------
1           2017-01-01 2017-01-04 10100.00              4
1           2017-01-05 2017-01-08 50100.00              4
1           2017-01-09 2017-01-31 5100.00               23
2           2017-01-01 2017-01-05 500.00                5
2           2017-01-06 2017-01-06 1500.00               1
2           2017-01-07 2017-01-07 3500.00               1
2           2017-01-08 2017-01-08 8500.00               1
2           2017-01-09 2017-01-31 4500.00               23