There are 3 Tables Customer, Invoice and Payment. Customer Link with Invoice and Payment table. Question is, need to retrieve statement wise Debit, Credit and Balance. main issue is opening balance not getting from date range. I provide sample data with queries. Customer Table: (When inserting a new customer, if they have pending balance assigning with opening balance otherwise keep blank (null))
CREATE TABLE BASE_Customer ( CustomerId INT IDENTITY(1,1), CustomerName VARCHAR(45), SalesOpeningBalance MONEY NULL, PRIMARY KEY(CustomerId) ) INSERT INTO BASE_Customer (CustomerName, SalesOpeningBalance) VAlUES ('Ricky', 2500) -- with opening balance. INSERT INTO BASE_Customer (CustomerName) VAlUES ('Smith') -- without opening balance.
Invoice Table:
CREATE TABLE BASE_Invoice ( InvoiceId INT IDENTITY(1,1), InvoiceDate DATE, CustomerId INT, Total MONEY, PRIMARY KEY(InvoiceId) ) INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-01', 1, 500) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-02', 2, 250) -- Smith INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-02', 1, 100) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-03', 1, 400) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-03', 2, 500) -- Smith INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-04', 1, 200) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-04', 2, 800) -- Smith INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-05', 1, 100) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-06', 1, 200) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-07', 1, 500) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-08', 1, 300) -- Ricky INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-08', 2, 400) -- Smith INSERT INTO BASE_Invoice (InvoiceDate, CustomerId, Total) VALUES ('2022-01-09', 1, 600) -- Ricky
PaymntTable:
CREATE TABLE BASE_Payment ( PaymentId INT IDENTITY(1,1), PaymentDate DATE, CustomerId INT, Total MONEY, PRIMARY KEY(PaymentId) ) INSERT INTO BASE_Payment (PaymentDate, CustomerId, Total) VALUES ('2022-01-03', 1, 400) -- Ricky INSERT INTO BASE_Payment (PaymentDate, CustomerId, Total) VALUES ('2022-01-05', 1, 500) -- Ricky INSERT INTO BASE_Payment (PaymentDate, CustomerId, Total) VALUES ('2022-01-05', 2, 300) -- Smith INSERT INTO BASE_Payment (PaymentDate, CustomerId, Total) VALUES ('2022-01-07', 1, 600) -- Ricky INSERT INTO BASE_Payment (PaymentDate, CustomerId, Total) VALUES ('2022-01-08', 2, 200) -- Smith
How to retrieve Output should below screenshots,
Picture 01: Customer (Ricky) Full Statement
Note: when created this customer given opening balance
Picture 02: Customer (Ricky) Date Range Statement
Note: Date Range Opening balance should be "previous records end balance" not when created this customer given opening balance above output picture 01 2,500
Picture 03: Customer (Smith) Full Statement
Note: when created this customer Not given opening balance so no need to appear opening balance
Picture 04: Customer (Smith) Date Range Statement
Note: Date Range Opening balance should be previous records end balance 750 (above full statement 1st two records total)
I tried with below query, but I didn't get expected result like above 4 screenshots, main issue is opening balance when retrieving date range.
--YYYY-MM-DD (Date Format) DECLARE @CustomerId INT = 2; SELECT [Date], Particulars, ABS(Debit) AS Debit, ABS(Credit) AS Credit, SUM(CASE WHEN Credit = 0 THEN Debit ELSE Credit END) OVER (ORDER BY [Date], [Particulars]) AS Balance FROM ( SELECT NULL AS [Date], 'OPENING BALANCE' AS [Particulars], ISNULL(SalesOpeningBalance, 0) AS [Debit], 0 AS [Credit], ISNULL(SalesOpeningBalance, 0) AS [Balance] FROM BASE_Customer WHERE (CustomerId = @CustomerId) UNION ALL SELECT BASE_Invoice.InvoiceDate AS [Date], 'INVOICE' AS [Particulars], BASE_Invoice.Total AS [Debit], 0 AS [Credit], ISNULL(BASE_Customer.SalesOpeningBalance, 0) AS [Balance] FROM BASE_Customer INNER JOIN BASE_Invoice ON BASE_Customer.CustomerId = BASE_Invoice.CustomerId WHERE (BASE_Customer.CustomerId = @CustomerId) --AND (BASE_Invoice.InvoiceDate BETWEEN '2022-01-04' AND '2022-01-05') UNION ALL SELECT BASE_Payment.PaymentDate AS [Date], 'PAYMENT' AS [Particulars], 0 AS [Debit], -1.0 * BASE_Payment.Total AS [Credit], ISNULL(BASE_Customer.SalesOpeningBalance, 0) AS [Balance] FROM BASE_Customer INNER JOIN BASE_Payment ON BASE_Customer.CustomerId = BASE_Payment.CustomerId WHERE (BASE_Customer.CustomerId = @CustomerId) --AND (BASE_Payment.PaymentDate BETWEEN '2022-01-04' AND '2022-01-05') ) u ORDER BY [Date]