Subtraction of inventory from Demand in BigQuery everday and adding new inventory

Here's how my data looks like:

date sku inventory_added demand
22nd Nov 2021 XYZ 70 18
23rd Nov 2021 XYZ 0 18
24th Nov 2021 XYZ 0 50
25th Nov 2021 XYZ 0 15
26th Nov 2021 XYZ 80 30
27th Nov 2021 XYZ 0 20
28th Nov 2021 XYZ 0 15
29th Nov 2021 XYZ 0 20
30th Nov 2021 XYZ 0 10
1st Dec 2021 XYZ 100 40
2nd Dec 2021 XYZ 0 10

I want to create a new column named solution using BigQuery SQL where in the 1st row, i.e. 22nd Nov 2021, I want formula as - inventory_added - demand. This will give me 1st row's value for solution will be 52.

Now what I am not able to do is from 2nd row: So, next now, will be 52 (remaining inventory from previous day) + 0 (inventory_added on 23rd Nov 2021) - 18 (demand on 23 Nov 2021). This is equal to 34.

Similarly going to next row, i.e. 24th November: value in solution will be 34 + 0 - 50 = -16. Now since it is negative, it should be put as 0.

I tried this - MAX(solutions, 0).

The result will look like this:

date sku inventory_added demand solution
22nd Nov 2021 XYZ 70 18 52
23rd Nov 2021 XYZ 0 18 34
24th Nov 2021 XYZ 0 50 0
25th Nov 2021 XYZ 0 15 0
26th Nov 2021 XYZ 80 30 50
27th Nov 2021 XYZ 0 20 30
28th Nov 2021 XYZ 0 15 15
29th Nov 2021 XYZ 0 20 0
30th Nov 2021 XYZ 0 10 0
1st Dec 2021 XYZ 100 40 60
2nd Dec 2021 XYZ 0 10 50

I am not sure if this can be accomplished by BigQuery, but all suggestions are welcome.

Thanks!



from Recent Questions - Stack Overflow https://ift.tt/3FHDlAM
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)