I have a Ledger
table:
CREATE TABLE Ledger
(
PersonID int,
Narration varchar(255),
Payment int(255)
);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (1, 'Snacks 1', 5);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (1, 'Snacks 2', 10);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (2, 'Snacks 3', 7);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (1, 'Snacks 4', 6);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (2, 'Snacks 5', 3);
INSERT INTO Ledger(PersonID, Narration, Payment)
VALUES (1, 'Snacks 6', 1);
The table looks like this:
PersonID Narration Payment
_____________________________________________
1 Snacks 1 5
1 Snacks 2 10
2 Snacks 3 7
1 Snacks 4 6
2 Snacks 5 3
1 Snacks 6 1
Here PersonID=1
totally spent 22 and PersonID=2
totally spent 10.
My requirement is to reduce the total Payment
to be below or equal to 20. There is no unique columns. I wish to delete records to make the total Payment
below or equal to 20.
In the above table, PersonID=1
has a total Payment
greater than 20, so I need to delete some records to reduce the total payment.
My Expected Output
PersonID Narration Payment
_____________________________________________
1 Snacks 2 10
2 Snacks 3 7
1 Snacks 4 6
2 Snacks 5 3
1 Snacks 6 1
Here I removed
1 Snacks 1 5
Now the total Payment
of PersonID=1
is 17, which is below 20.
Based on logic we have to delete the records.
Kindly assist me in both SQL Server and MySQL. My First Preference is SQL Server.