I have a table
Orders
ID_Order (autoNum) |
ID_Cust (int) |
amountOrdered (int) |
amountPerPackage (int) |
1 |
1 |
20 |
10 |
2 |
1 |
10 |
5 |
3 |
1 |
5 |
5 |
And I want my output query to look like this:
packageNo |
ID_Order |
ID_Cust |
1 |
1 |
1 |
2 |
1 |
1 |
3 |
2 |
1 |
4 |
2 |
1 |
5 |
3 |
1 |
Here is a short pseudo-code (same result can be achieved in a few different ways using code).
Code:
counter: Int
remainder: Int
counter = 0
remainder = 0
For (OrderID, CustID) in Orders:
While (remainder != 0):
remainder = amountOrdered - amountPerPackage
amountOrdered = amountOrdered - amountPerPackage
counter = counter + 1
output: counter, OrderID, CustID
Loop
Loop
My question, however, is, how do you achieve this using SQL?
Basically I want the output query to be used in a report to print out the packaging labels.