I have a simple table. Here's an example
![Name: Screenshot 2023-08-02 at 13.19.35.jpg
Views: 59
Size: 20.7 KB]()
If I want the most recent n records where PartNumber is P001. But I don't want all the rows. Just the most recent 10 parts. ie where the the sum of the quantities is, for example, 10. So from the table I'd want rows 9, 7 and 6. The sum of the quantities of those three rows is 12. So they're the rows I want.
Currently I do that with a simple SQL SELECT query that reads backwards looking at the Quantity till I reach the total quantity I'm looking for. This works pretty quick but I was just wondering. Is there an SQL query that would just read the rows necessary.
Or is reading one row at a time till I get enough just as efficient. The table might have 1000's of rows and the total quantity I want might be hundreds so that'd be a lot loops.
Sorry. Not sure if I explained this clearly
Anyway. Something like:-
SELECT * FROM SOME_TABLE WHERE PartNumber = 'P001' AND SUM(Quantity) => 10 ORDER BY ID DESC
Actually I haven't tested that SQL because it just looks stupid. But as psudo code I hope it explains what I'm wanting to do.
If I want the most recent n records where PartNumber is P001. But I don't want all the rows. Just the most recent 10 parts. ie where the the sum of the quantities is, for example, 10. So from the table I'd want rows 9, 7 and 6. The sum of the quantities of those three rows is 12. So they're the rows I want.
Currently I do that with a simple SQL SELECT query that reads backwards looking at the Quantity till I reach the total quantity I'm looking for. This works pretty quick but I was just wondering. Is there an SQL query that would just read the rows necessary.
Or is reading one row at a time till I get enough just as efficient. The table might have 1000's of rows and the total quantity I want might be hundreds so that'd be a lot loops.
Sorry. Not sure if I explained this clearly
Anyway. Something like:-
SELECT * FROM SOME_TABLE WHERE PartNumber = 'P001' AND SUM(Quantity) => 10 ORDER BY ID DESC
Actually I haven't tested that SQL because it just looks stupid. But as psudo code I hope it explains what I'm wanting to do.