0

我有一个表格,其中包含如下数据。INVENTORY_ITEM_ID 是项目的唯一 ID,TYPE_QTY 是唯一年龄桶 (AGE_IN_DAYS) 中项目的总数量,RUNNING_TOTAL 是基于年龄桶上的 TYPE_QTY 计算得出的列。

在此处输入图像描述

每个负数都需要从最后一组行中删除。例如,在第一次出现负数为-508 时,应识别并调整第一个具有满足发行508 的运行总计的行,如下所示。该行上方的所有行都应从结果集中删除。

在此处输入图像描述

RUNNING_TOTAL 和 TYPE_QTY 列使用 (555-508) 的余额进行调整,循环继续。第二次发布 -22 数量发生在第一行,因为它的运行总数为 47,给定数据的最终结果应如下所示

在此处输入图像描述

我已经制作了一个可以完成这项工作的 PL/SQL 块,但是更愿意使用纯 SQL 来实现它。我目前的 SQL 技能还不够。

PL/SQL 块

SET SERVEROUTPUT ON;

DECLARE

CURSOR INVDATA IS
SELECT tx.*
from OMSINVDT_TEMP tx
--where inventory_item_id = 35253
order by inventory_item_id,age_in_days desc;

CURSOR inline_data(p_item_id IN NUMBER) IS
SELECT inventory_item_id,
type_qty,
age_in_days,
SUM (type_qty) OVER ( PARTITION BY inventory_item_id ORDER BY age_in_days desc) RUNNING_TOTAL
FROM omsinvdata_temp
where inventory_item_id = p_item_id;

l_line_qty number;
l_last_age_period number := 0;

BEGIN

execute immediate 'truncate table omsinvdata_temp';

for i in invdata loop
--if the qty is greater than 0, add to the temp table
if i.type_qty > 0 then
        insert into omsinvdata_temp(
        inventory_item_id ,
        type_qty ,
        age_in_days ,
        running_total )
        values(i.inventory_item_id, i.type_qty, i.age_in_days, 0);
else
--if the quantity is negative
--open the cursor for the item from temporary table
--and find the row that can satisfy the negative quantity
--dbms_output.put_line('current quantity: '||i.type_qty);
for j in inline_data(i.inventory_item_id) loop
--dbms_output.put_line('Line Qty '||j.type_qty||' Running total: '||j.running_total||' To Issue: '||i.type_qty||' Bucket '||j.age_in_days);


if (abs(i.type_qty)>j.running_total) then
  --  dbms_output.put_line('Running total: '||j.running_total||' not sufficient to issue '||i.type_qty||' Bucket '||j.age_in_days);
    update omsinvdata_temp
    set type_qty =0,
    running_total =0
    where age_in_days = j.age_in_days
    and inventory_item_id = i.inventory_item_id;
    else
  --  dbms_output.put_line('Running total: '||j.running_total||' sufficient to issue '||i.type_qty||' Bucket '||j.age_in_days);
    update omsinvdata_temp
    set type_qty = j.running_total + i.type_qty,
    running_total = j.running_total + i.type_qty
    where age_in_days = j.age_in_days
    and inventory_item_id = i.inventory_item_id;
    exit;
 end if;

end loop;    
end if;
end loop;


commit;


END;

使用显示的数据创建表脚本

CREATE TABLE "OMSINVDT_TEMP" ("INVENTORY_ITEM_ID" NUMBER, "TYPE_QTY" NUMBER, "AGE_IN_DAYS" NUMBER, "RUNNING_TOTAL" NUMBER)
REM INSERTING into OMSINVDT_TEMP
SET DEFINE OFF;
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,72,6,72);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,384,5,456);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,105,4,561);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,-512,3,49);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,-24,2,25);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (35253,134,1,159);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (266234,2,4,2);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (266234,1,3,3);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (266234,-1,2,2);
Insert into OMSINVDT_TEMP (INVENTORY_ITEM_ID,TYPE_QTY,AGE_IN_DAYS,RUNNING_TOTAL) values (266234,-2,1,0);
commit;

我已经向 Oracle SQL/PLSQL 社区提出了这个问题,但未能解释构造逻辑。

https://community.oracle.com/tech/developers/discussion/4480421/sql-match-quantity-and-pick-rows

4

1 回答 1

1

这是一种方法,仅使用分析函数和聚合。您没有解释AGE_IN_DAYS输出中的列-根据您的示例,我假设它表示最后一个负行之前的最新行的年龄。

RUNNING_TOTAL列不应存在于输入中,因为它是根据其他数据计算得出的。即使你有它在表中,我忽略它 - 我直接计算它。(我假设您显示的不是您真正的起始数据,而是您无法继续解决方案的点。)

INSERT您使用的示例与您的陈述之间也存在不匹配。我按INSERT原样使用这些语句(其中一行的值不同);这解释了为什么我的输出看起来与你的不同。

主要技巧在WITH子句中,在PREP子查询中。我为最后一个“负”行之后的行分配了一个标志。然后在主查询中,我按此标志分组,除此之外,仅当设置标志时,按 AGE_IN_DAYS。这样,直到并包括最后一个“负”行的所有行都在一组中,而其余的正行是每组一行。(我假设AGE_IN_DAYS每个人都是不同的INVENTORY_ITEM_ID;如果不是,例如,我可以使用其他东西ROWNUM- 但无论如何问题都不会得到很好的定义。)

所以,就在这里。如果您有任何问题,请查看并告诉我。

with prep as (
  select inventory_item_id, type_qty, age_in_days,
         case count(case when type_qty < 0 then 1 end) 
              over (partition by inventory_item_id order by age_in_days)
              when 0 then 'Y' end as past_last_negative
  from   omsinvdt_temp
)
select inventory_item_id, sum(type_qty) as type_qty,
       min(case when type_qty > 0 then age_in_days end) as age_in_days,
       sum(sum(type_qty)) over (partition by inventory_item_id
                                order by max(age_in_days)) as running_total
from   prep
group  by inventory_item_id,
          case past_last_negative when 'Y' then age_in_days end
order  by inventory_item_id, age_in_days desc
;

INVENTORY_ITEM_ID   TYPE_QTY AGE_IN_DAYS RUNNING_TOTAL
----------------- ---------- ----------- -------------
            35253         25           4           159
            35253        134           1           134
           266234          0           3             0
于 2021-02-12T17:56:15.677 回答