Tables are INVOICE, PAYMENT, RETURN_INVOICE, RETURN_PAYMENT, and many others in my database, i need to retrieve knowledge CREDIT, DEBIT AND BALANCE buyer smart and order ought to be DATE smart and desk insertion order, for instance 1st i’ve entered bill after i’ve entered fee after that once more fee after insert new bill like that order
Bill Desk there are 2 sorts of Invoices “CASH” and “CREDIT”
Bill Fee Desk there are three sorts of Fee strategies “CASH”, “ADVANCE”, “CREDIT”
if Money bill made Money Fee executing Identical time.
If Credit score bill made Advance fee could also be or not executing similar time (some occasions advance fee made in any other case whole bill ought to be credit score).
under are tables and insert question statements,
CREATE TABLE Buyer
(
CustomerId INT IDENTITY(1,1),
CustomerName VARCHAR(45),
OpeningBalance MONEY,
PRIMARY KEY(CustomerId)
)
INSERT INTO Buyer (CustomerName, OpeningBalance) VAlUES ('DEMI', 1000)
CREATE TABLE Bill
(
InvoiceId INT IDENTITY(1,1),
InvoiceDate DATE,
CustomerId INT,
InvoiceMethod VARCHAR(45) NOT NULL, -- CASH or CREDIT
InvoiceTotal MONEY,
PRIMARY KEY(InvoiceId)
)
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-16', 1, 'CASH', 1000);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-16', 1, 'CREDIT', 2000);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-17', 1, 'CREDIT', 500);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-17', 1, 'CREDIT', 2000);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-18', 1, 'CASH', 150);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-18', 1, 'CREDIT', 1000);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-22', 1, 'CREDIT', 2250);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-24', 1, 'CREDIT', 1750);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-28', 1, 'CREDIT', 3000);
INSERT INTO Bill (InvoiceDate, CustomerId, InvoiceMethod, InvoiceTotal) VAlUES ('2020-01-28', 1, 'CREDIT', 1000);
CREATE TABLE Fee
(
PaymentId INT IDENTITY(1,1),
PaymentDate DATE,
CustomerId INT,
PaymentMethod VARCHAR(45) NOT NULL, -- CASH or CREDIT, ADVANCE
PaymentTotal MONEY,
PRIMARY KEY(PaymentId)
)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-16', 1, 'CASH', 1000)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-17', 1, 'ADVANCE', 250)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-18', 1, 'CREDIT', 1500)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-18', 1, 'CREDIT', 750)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-18', 1, 'CASH', 150)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-20', 1, 'CREDIT', 2000)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-23', 1, 'CREDIT', 5000)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-26', 1, 'CREDIT', 200)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-28', 1, 'CREDIT', 500)
INSERT INTO Fee (PaymentDate, CustomerId, PaymentMethod, PaymentTotal) VAlUES ('2020-01-28', 1, 'ADVANCE', 250)
CREATE TABLE ReturnInvoice
(
ReturnInvoiceId INT IDENTITY(1,1),
ReturnInvoiceDate DATE,
CustomerId INT,
ReturnInvoiceTotal MONEY,
PRIMARY KEY(ReturnInvoiceId)
)
INSERT INTO ReturnInvoice (ReturnInvoiceDate, CustomerId, ReturnInvoiceTotal) VAlUES ('2020-01-25', 1, 500)
INSERT INTO ReturnInvoice (ReturnInvoiceDate, CustomerId, ReturnInvoiceTotal) VAlUES ('2020-01-28', 1, 300)
CREATE TABLE ReturnPayment
(
ReturnPaymentId INT IDENTITY(1,1),
ReturnPaymentDate DATE,
CustomerId INT,
ReturnPaymentTotal MONEY,
PRIMARY KEY(ReturnPaymentId)
)
INSERT INTO ReturnPayment (ReturnPaymentDate, CustomerId, ReturnPaymentTotal) VAlUES ('2020-01-21', 1, 500)
INSERT INTO ReturnPayment (ReturnPaymentDate, CustomerId, ReturnPaymentTotal) VAlUES ('2020-01-27', 1, 2000)
I need to retrieve the output like this, (Order ought to be similar like display screen shot)