简体   繁体   中英

Create calculated field based on previous rows Oracle SQL

I am trying to create a manually calculated column where I keep track of a current inventory.

Currently, I have a table that looks like this:

| Group        | Part | Operation Type | Transaction Amount |
|--------------|------|----------------|--------------------|
| Concrete     | A    | STOCK          | 100                |
| Concrete     | A    | Buy            | 25                 |
| Concrete     | A    | Make           | -10                |
| Concrete     | A    | Make           | -10                |
| Concrete     | A    | Make           | -10                |
| Concrete     | A    | Make           | -10                |
| Concrete     | A    | Make           | -10                |
| Concrete     | B    | STOCK          | -10                |
| Concrete     | B    | Make           | -10                |
| Concrete     | B    | Make           | -10                |
| Concrete     | B    | Make           | -10                |
| Concrete     | B    | Make           | -10                |
| Concrete     | B    | Make           | 150                |
| Construction | C    | STOCK          | 10                 |
| Construction | C    | Make           | -1                 |
| Construction | C    | Make           | -1                 |
| Construction | C    | Make           | -1                 |
| Construction | C    | Make           | -1                 |
| Construction | D    | STOCK          | 5                  |
| Construction | D    | Make           | -5                 |

The table is first ordered by group then by part , and then STOCK is always shown as the first value. The idea is to create a new manually calculated column, curr_inventory , that allows for us to keep track of current inventory and see if or when our inventory for a given part, for a given group, dips below 0.

Ideally, the end results would look like this:

|     Group    | Part | Operation Type | Transaction Amount | New_Inventory_Column |
|:------------:|:----:|:--------------:|:------------------:|:--------------------:|
|   Concrete   |   A  |      STOCK     |         100        |          100         |
|   Concrete   |   A  |       Buy      |         25         |          125         |
|   Concrete   |   A  |      Make      |         -10        |          115         |
|   Concrete   |   A  |      Make      |         -10        |          105         |
|   Concrete   |   A  |      Make      |         -10        |          95          |
|   Concrete   |   A  |      Make      |         -10        |          85          |
|   Concrete   |   A  |      Make      |         -10        |          75          |
|   Concrete   |   B  |      STOCK     |         10         |          10          |
|   Concrete   |   B  |      Make      |         -10        |           0          |
|   Concrete   |   B  |      Make      |         -10        |          -10         |
|   Concrete   |   B  |      Make      |         -10        |          -20         |
|   Concrete   |   B  |      Make      |         -10        |          -30         |
|   Concrete   |   B  |      Make      |         150        |          120         |
| Construction |   C  |      STOCK     |         10         |          10          |
| Construction |   C  |      Make      |         -1         |           9          |
| Construction |   C  |      Make      |         -1         |           8          |
| Construction |   C  |      Make      |         -1         |           7          |
| Construction |   C  |      Make      |         -1         |           6          |
| Construction |   D  |      STOCK     |          5         |           5          |
| Construction |   D  |      Make      |         -5         |           0          |

The end result would be a column that initiates when the part number has changed and the operation type is STOCK, and then begins to calculate (using the transaction amount) what the current inventory is. I am not sure where to start on a SQL query that would allow for this. Intuitively, the pseudocode would look something like:

for each row in table:
    if operation_type == "stock":
        curr_inv = stock.value
    else:
        curr_inv = previous_curr_inv + transaction_amount

However, I am not sure how to even begin writing SQL for this. I typically try to post what SQL I am working with but I don't even know where to begin. I have looked at various posts online, on SO, including posts like this , and this , and this , but I could not see how the selected answers could be used as a solution.

I used the window function to calculate the running total.

I added the row_number column in the subquery.

Try this:

select t1."Group",t1."Part",t1."Operation Type", t1."Transaction Amount",
sum(t1."Transaction Amount") over (partition by t1."Group",t1."Part" order by t1.rownumber)
from (
select row_number() over (order by null) as rownumber, t.*
from test t ) t1

Test Result:

DB<>Fiddle

SQL tables represent unordered sets. To put things in order, you need a column with the ordering. In the code below, I will use ? for this column.

Then you want a cumulative sum:

select t.*,
       sum(case when operation_type in ('STOCK'),
                when operation_type in ('Make') then - amount
                when operation_type in ('Buy') then - amount
                else 0
           end) over (partition by group, part order by ?)
from t;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM