How to deduce the initial date from the total value with esProc

A certain database table records the planned inbound quantity and inventory after inbound on a specific date, such as the planned inbound quantity of 0.6 on February 26th, resulting in a total inventory of 3.

ITEM

LOC

NEEDDATE

QTY

CUSTQTY

ABC

XYZ

2024-02-13 00:00:00

4.8

3

ABC

XYZ

2024-02-14 00:00:00

0.6

3

ABC

XYZ

2024-02-15 00:00:00

0.6

3

ABC

XYZ

2024-02-16 00:00:00

0.6

3

ABC

XYZ

2024-02-19 00:00:00

0.6

3

ABC

XYZ

2024-02-20 00:00:00

0.6

3

ABC

XYZ

2024-02-21 00:00:00

0.6

3

ABC

XYZ

2024-02-22 00:00:00

0.6

3

ABC

XYZ

2024-02-23 00:00:00

0.6

3

ABC

XYZ

2024-02-26 00:00:00

0.6

3

ABC

XYZ

2024-02-27 00:00:00

0.6

3

ABC

XYZ

2024-02-28 00:00:00

0.6

3

ABC

XYZ

2024-02-29 00:00:00

0.6

3

Now, based on the given date, we need to use the total inventory and the planned inbound inventory to deduce the initial date, which is the day when there is zero or negative inventory. We need to add the daily inventory before inbound as UPDATED_CUSTQTY and return the complete data. For example, given February 26th, it can be known that the original inventory of the day before inbound was 3-0.6=2.4; The previous date was February 23rd, and the original inventory on that day was 2.4-0.6=1.8; Until February 20th, the original inventory for that day was 0.

ITEM

LOC

NEEDDATE

QTY

CUSTQTY

UPDATED_CUSTQTY

ABC

XYZ

2024-02-29 00:00:00

0.6

3

ABC

XYZ

2024-02-28 00:00:00

0.6

3

ABC

XYZ

2024-02-27 00:00:00

0.6

3

ABC

XYZ

2024-02-26 00:00:00

0.6

3

2.4

ABC

XYZ

2024-02-23 00:00:00

0.6

3

1.8

ABC

XYZ

2024-02-22 00:00:00

0.6

3

1.2

ABC

XYZ

2024-02-21 00:00:00

0.6

3

0.6

ABC

XYZ

2024-02-20 00:00:00

0.6

3

0.0

ABC

XYZ

2024-02-19 00:00:00

0.6

3

ABC

XYZ

2024-02-16 00:00:00

0.6

3

ABC

XYZ

2024-02-15 00:00:00

0.6

3

ABC

XYZ

2024-02-14 00:00:00

0.6

3

ABC

XYZ

2024-02-13 00:00:00

4.8

3

SQL lacks natural sequence numbers and convenient syntax to express relative positions, requiring the indirect implementation of multiple window functions, making it difficult to write code. SPL can directly represent relative or absolute position:https://try.esproc.com/splx?2Nq


 A

1

$select *, null as UPDATED_CUSTQTY from table_name.txt order by NEEDDATE desc

2

=A1.select(NEEDDATE<=date("2024-02-26"))

3

=A2.(UPDATED_CUSTQTY=round(if(#==1,CUSTQTY-QTY,if((t=UPDATED_CUSTQTY[-1]-QTY)>=0,t)),1))

4

return A1

A1 Load data, sort in reverse order by date, add a calculated column for inventory before inbound UPDATED_CUSTQTY, with an initial value of null.

A2 Filter out records before the specified date that reference A1's records, and modifying these records will affect A1.

Picture1png
A3: Loop modifying each record in A2: If it is currently the first record, then original inventory=total inventory - received quantity, otherwise it is equal to "previous original inventory - received quantity", until this value is less than 0, and the result is rounded to one decimal place. [-1] represents the previous record. The modified A2 is as follows:

Picture2png
A4: Return the complete data A1, where the red box represents A2.

Picture3png