SQL,计算库存容量

SQLite 的 process_table 表存储库存信息,arrivals是到货吨数,max_output_capacity 是最大库存吨数,理论上当天的 arrivals 要小于 max_output_capacity,否则不能完全入库,要留到第二天再入库。

day

arrivals

max_output_capacity

0

0

2

1

2

3

2

5

4

3

0

5

4

0

5

5

14

1

6

0

3

7

1

2

8

1

12

请增加一个计算列remaining_next_day,用来计算当天的不能入库,要留到第二天的吨数。

day

arrivals

max_output_capacity

remaining_next_day

0

0

2

0

1

2

3

0

2

5

4

1

3

0

5

0

4

0

5

0

5

14

1

13

6

0

3

10

7

1

2

9

8

1

12

0

编写SPL代码:

=sqliteDB.query("select * from process_table order by day")
.derive(if((t=remaining_next_day[-1]+arrivals-max_output_capacity)>0,t,0):remaining_next_day)

函数derive新增计算列,[-1]表示上一条记录。

来源:https://www.reddit.com/r/SQL/comments/1c865t8/query_to_calculate_the_remaining_units_to_the/