我有一个表格,其中包含如下数据。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