Get an initial balance

I have this table along with sample data. I want to calculate the initial balance from the data available in the database.

DROP TABLE VoucherPayment;
CREATE TABLE VoucherPayment
(
VoucherID Int,
VoucherName nvarchar(25),
Credit int,
Debit int,
VoucherDate date
);

INSERT INTO VoucherPayment VALUES (1, 'Cash Payment', '2000', NULL, '2017-12-09');
INSERT INTO VoucherPayment VALUES (2, 'Cash Payment', NULL, '1900', '2017-12-15');
INSERT INTO VoucherPayment VALUES (3, 'Cash Payment', '5680', NULL, '2017-12-19');
INSERT INTO VoucherPayment VALUES (4, 'Cash Receipt', '1200', NULL, '2017-12-20');

I wrote a query that gives a way out to some extent, mentioned below.

WITH CTE AS (
    SELECT 
    VoucherID, VoucherName, SUM(Debit) [DrAmount], SUM(Credit) [CrAmount]

    FROM 
    VoucherPayment

    WHERE 
    [VoucherDate] BETWEEN CAST('Dec  1 2017 12:00AM' AS DATE) AND CAST('Dec 30 2017 12:00AM' AS DATE)
    GROUP BY 
    VoucherName, VoucherID

)
SELECT *,
        SUM(ISNULL(DrAmount, 0)+ISNULL(CrAmount, 0)) OVER (ORDER BY VoucherID 
                  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as Amount

FROM CTE

The output of this request

VoucherID   VoucherName               DrAmount    CrAmount    Amount
----------- ------------------------- ----------- ----------- -----------
1           Cash Payment              NULL        2000        2000
2           Cash Payment              1900        NULL        3900
3           Cash Payment              NULL        5680        9580
4           Cash Receipt              NULL        1200        10780

If I change the date until December 19, I get this conclusion

VoucherID   VoucherName               DrAmount    CrAmount    Amount
----------- ------------------------- ----------- ----------- -----------
3           Cash Payment              NULL        5680        5680
4           Cash Receipt              NULL        1200        6880

Instead, I want it to be like

VoucherID   VoucherName      DrAmount    CrAmount    Amount    Opening
----------- ---------------- ----------- ----------- --------- ---------
3           Cash Payment     NULL        5680        9580      3900
4           Cash Receipt     NULL        1200        10780     9580
+4
source share
1 answer

Try the following:

;WITH CTE AS (
    SELECT 
    VoucherID,VoucherDate, VoucherName, SUM(Debit) [DrAmount], SUM(Credit) [CrAmount]
   FROM VoucherPayment
   --WHERE [VoucherDate] BETWEEN CAST('Dec  01 2017 12:00AM' AS DATE) AND CAST('Dec 30 2017 12:00AM' AS DATE)
    GROUP BY VoucherName,VoucherDate, VoucherID
)
SELECT VoucherID, VoucherName,D.DrAmount,D.CrAmount,D.Amount,D.Amount-D.CrAmount [Opening]
FROM(
    SELECT *,
            SUM(ISNULL(DrAmount, 0)+ISNULL(CrAmount, 0)) OVER (ORDER BY VoucherID 
                     ) as Amount
    FROM CTE
    )D
WHERE [VoucherDate] BETWEEN CAST('Dec  19 2017 12:00AM' AS DATE) AND CAST('Dec 30 2017 12:00AM' AS DATE)

Conclusion:

VoucherID   VoucherName     DrAmount    CrAmount    Amount  Opening
3           Cash Payment    NULL        5680        9580    3900
4           Cash Receipt    NULL        1200        10780   9580
+1
source

Source: https://habr.com/ru/post/1695408/


All Articles