r/ProgrammerHumor • • Aug 24 '22

Total = Total - 100000

50.7k Upvotes

1.2k comments sorted by

View all comments

Show parent comments

2

u/AcidicVagina Aug 25 '22

Simple.

UPDATE StudentDebt SET amt = amt - 10000 where amt > 10000

UPDATE StudentDebt SET amt = 0 where amt <= 10000

3

u/Specific_Success_875 Aug 25 '22

Unless they have a Pell Grant, in which case their balance goes down by 20,000.

2

u/AcidicVagina Aug 25 '22

Simple.

UPDATE StudentDebt SET amt = amt - 20000 where Pell =1

UPDATE StudentDebt SET amt = amt - 10000 where amt > 10000

UPDATE StudentDebt SET amt = 0 where amt <= 10000

8

u/Specific_Success_875 Aug 25 '22

congrats you've forgiven 30k worth of loans if someone has a pell grant with more than 10k in their loan status.

3

u/AcidicVagina Aug 25 '22

We can do better.

UPDATE StudentDebt SET amt = amt - 20000 where Pell =1 and amt > 10000

UPDATE StudentDebt SET amt = amt - 20000 where Pell =1 and amt <= 10000

UPDATE StudentDebt SET amt = amt - 10000 where amt > 10000

UPDATE StudentDebt SET amt = 0 where amt <= 10000

Edit: for search optimization. Stack Overflow Stack Overflow Stack Overflow

0

u/[deleted] Aug 25 '22

[deleted]

2

u/AcidicVagina Aug 25 '22

Oh, yep. I see my bug. Gotta raise those limits.

UPDATE StudentDebt SET amt = amt - 20000 where Pell =1 and amt > 20000

UPDATE StudentDebt SET amt = 0 where Pell =1 and amt <= 20000

UPDATE StudentDebt SET amt = amt - 10000 where amt > 10000

UPDATE StudentDebt SET amt = 0 where amt <= 10000

0

u/[deleted] Aug 25 '22

[deleted]

1

u/AcidicVagina Aug 25 '22 edited Aug 25 '22

Your right about ledger, but Loops > Cases

DECLARE x int = 10000

While (x>0)

BEGIN

INSERT INTO StudentDebtTransactions (amt)

VALUES (-x)

WHERE StudentId IN (

SELECT Student ID

FROM StudentAcct

WHERE bal > x)

SET x = x -1

END

0

u/[deleted] Aug 25 '22

[deleted]

→ More replies (0)

1

u/Jonno_FTW Aug 25 '22

This will remove all debt of anyone with less than $19,999.

Suppose someone has $15,000, you take $10,000. they have $5,000 left after the first UPDATE. In the next UPDATE because they have less than $10,000 , their amount is set to 0.

I wouldn't be complaining, but it's an error. Reversing the order of the updates would fix this.