Home Database Mysql Tutorial Oracle数据分摊问题解析

Oracle数据分摊问题解析

Jun 07, 2016 pm 05:58 PM
oracle business data parse question

经常会碰到,由于业务需要,需要将某种汇总的数据按照一定的原则分摊给一堆数据。 其实,如果逻辑清晰的话,这类型的程序还是比较好些的。 本文重点是如果用简单的程序实现这种效果,而且不容易分摊分错。 所有的分摊问题,首先必须要搞清楚以下几点问题: 1

经常会碰到,由于业务需要,需要将某种汇总的数据按照一定的原则分摊给一堆数据。
其实,如果逻辑清晰的话,这类型的程序还是比较好些的。
本文重点是如果用简单的程序实现这种效果,而且不容易分摊分错。

所有的分摊问题,首先必须要搞清楚以下几点问题:
1 首要的,要确定 什么东西,多少数量 分摊给什么东西?举个形象的例子,一桶沙子分摊给一些瓶子。
2 分摊的先后原则。上面的例子,一桶沙子分摊给一些瓶子,那瓶子的被分摊顺序是什么样子的?沙子先给哪个瓶子?要先确定清楚。

说得好像有点麻烦,举个例子说明。
最近接到的一个需求:
PO入库的时候,批次可能重复输入,所以入库之后,库存已经汇总在一起了。然后用户对(汇总的)库存进行消耗(就是杂发)。
现在需要有个报表可以知道:按照先进先出的原则,区分用户的一段期间内的消耗数量 对应的是那笔入库单号。

备注:假设下面的数量对应都是主单位。

7.1 入库单R1  料号A 批次P1 接收入库 400
7.3 入库单R2  料号A 批次P1 接收入库 300
这时候,P1批次库存共 700
-------消耗(杂发)明细
7.10 消耗P1 100
7.12 消耗P1 200
8.10 消耗P1 200
8.13 消耗P1 100
8.20 消耗P1 50
9.20 消耗P1 50


如果查询报表的日期选择的是:8.1~8.31
     8.1号 之前共消耗100+200=300
8.1~8.31号 之内一共消耗:200+100+50=350

所以核心问题是要将350如何分摊在R1和R2里面。
要实现的分摊效果:
      入库总数      之前消耗的分摊    期间内消耗的分摊
R1       400           300                  100
R2       300            0                   250

所以,结果是,报表是:8.1~8.31
一共消耗350,对应入库单的消耗情况:
R1消耗100
R2消耗250

实现逻辑:
你可以假想,现在有2个沙桶,
红色的沙桶装的沙子是 之前消耗的分摊 的数量
黑色的沙桶装的沙子是 期间内消耗的分摊 的数量
每张入库单就是一个瓶子,所以共有2个瓶子,R1和R2。现在是如何将 红色的沙子 和 黑色的沙子 装到这2个瓶子里面。

装沙规则:
1 用沙子的顺序:先用 红色的沙子,用完之后,再用黑色的沙子。
2 装瓶子的顺序:按照先进先出的原则,必须先装瓶子R1,再装R2.



DECLARE
  L_PRE_PERIOD_QTY NUMBER; ---期间前的汇总消耗量 ---之前消耗的分摊 的数量---红色的沙子
  L_CURR_PERIOD_QTY NUMBER ; --本期的汇总消耗量--期间内消耗的分摊 的数量---黑色的沙子
  ----装的结果用记录类型存下来,因为后面要用到。
   TYPE shipment_consume_Rec_Type   IS RECORD
   (
     SHIPMENT_LINE_ID  NUMBER
   , PRIMARY_QUANTITY             NUMBER
   , consume_pre_qty NUMBER
   , consume_curr_qty NUMBER
      );
    TYPE shipment_consume_Tbl_Type IS TABLE OF shipment_consume_Rec_Type
    INDEX BY BINARY_INTEGER ;
    L_shipment_consume_Tbl shipment_consume_Tbl_Type;
    N NUMBER;
BEGIN
  ----1 首先要算出红色的沙子和黑色的沙子的总数量,就是有多少数量可分摊。
SELECT nvl(sum(case when MMT.transaction_date      ABS(NVL(MTLN.PRIMARY_QUANTITY,0))
     else
       0
     end ),0) PRE_PERIOD_QTY,
     nvl(sum(case when MMT.transaction_date >= :P_F_START_DATE then
     ABS(NVL(MTLN.PRIMARY_QUANTITY,0))
     else
       0
     end ),0) CURR_PERIOD_QTY
   INTO L_PRE_PERIOD_QTY,L_CURR_PERIOD_QTY
  FROM MTL_MATERIAL_TRANSACTIONS MMT
      ,MTL_TRANSACTION_LOT_NUMBERS MTLN
WHERE MMT.TRANSACTION_ID = MTLN.TRANSACTION_ID
   AND MMT.TRANSACTION_TYPE_ID = 74
   AND MMT.TRANSACTION_ACTION_ID = 6
   AND MMT.OWNING_TP_TYPE = 1 ---所有权转出的(寄售供应商的库存)
   ---
   AND MMT.ORGANIZATION_ID = 103
   AND MMT.INVENTORY_ITEM_ID = 11783561
   AND MTLN.LOT_NUMBER = 'P0000001'
   AND MMT.transaction_date    DBMS_OUTPUT.PUT_LINE('L_PRE_PERIOD_QTY:'||L_PRE_PERIOD_QTY||' -L_CURR_PERIOD_QTY:'||L_CURR_PERIOD_QTY);
   N := 1;

   -----2 分摊主逻辑。
   FOR REC_SHIPMENT_LINE IN (
       -----瓶子(入库单)的游标
    SELECT MMT.ORGANIZATION_ID
        ,MMT.INVENTORY_ITEM_ID
        ,MTLN.LOT_NUMBER
        ,MTLN.TRANSACTION_DATE
        ,RT.SHIPMENT_HEADER_ID
        ,RT.SHIPMENT_LINE_ID
        ,MTLN.PRIMARY_QUANTITY
    FROM MTL_TRANSACTION_LOT_NUMBERS MTLN, MTL_MATERIAL_TRANSACTIONS MMT,RCV_TRANSACTIONS RT
   WHERE MTLN.TRANSACTION_ID = MMT.TRANSACTION_ID
     AND RT.TRANSACTION_ID = MMT.RCV_TRANSACTION_ID
     AND MMT.TRANSACTION_TYPE_ID = 18
     AND MMT.TRANSACTION_SOURCE_TYPE_ID = 1
     AND XYG_PO_PKG.CHECK_PO_LINE_CONSIGN(RT.PO_LINE_ID) = 'Y'
     AND MMT.ORGANIZATION_ID = 103
     AND MMT.INVENTORY_ITEM_ID = 11783561
     AND MTLN.LOT_NUMBER = 'P0000001'
     ORDER BY MTLN.TRANSACTION_DATE,MMT.TRANSACTION_ID) LOOP
       ---2.1 优先消耗期初之前的耗料数量,就是先用红色的沙子的数量。
       IF L_PRE_PERIOD_QTY >= REC_SHIPMENT_LINE.PRIMARY_QUANTITY THEN ---当红色沙子的数量大于瓶子的容量的时候。
         L_shipment_consume_Tbl(N).SHIPMENT_LINE_ID :=REC_SHIPMENT_LINE.SHIPMENT_LINE_ID;
         L_shipment_consume_Tbl(N).PRIMARY_QUANTITY :=REC_SHIPMENT_LINE.PRIMARY_QUANTITY;
         ----消耗红沙的数量就是瓶子的容量。
         L_shipment_consume_Tbl(N).consume_pre_qty :=REC_SHIPMENT_LINE.PRIMARY_QUANTITY;
         L_shipment_consume_Tbl(N).consume_curr_qty :=0;
         ----期初数量就是剩下要分配的数量。因为红色沙子已经被消耗掉一部分了。
         L_PRE_PERIOD_QTY :=L_PRE_PERIOD_QTY-REC_SHIPMENT_LINE.PRIMARY_QUANTITY;
       ELSE ---当红色沙子的数量小于瓶子容量的时候
         L_shipment_consume_Tbl(N).SHIPMENT_LINE_ID :=REC_SHIPMENT_LINE.SHIPMENT_LINE_ID;
         L_shipment_consume_Tbl(N).PRIMARY_QUANTITY :=REC_SHIPMENT_LINE.PRIMARY_QUANTITY;
         ----瓶子装 红色沙子的数量就是红色沙子的数量了
         L_shipment_consume_Tbl(N).consume_pre_qty :=L_PRE_PERIOD_QTY;

         ----这时候已经用完红色沙子了,开始用黑色沙子了-----
           ---2.2 当黑色沙子数量大于瓶子 可用的容量 的时候。
         IF L_CURR_PERIOD_QTY > (REC_SHIPMENT_LINE.PRIMARY_QUANTITY - L_PRE_PERIOD_QTY) THEN
           ---该瓶子 装黑色沙子的数量 就是 瓶子的可用容量。
           L_shipment_consume_Tbl(N).consume_curr_qty := REC_SHIPMENT_LINE.PRIMARY_QUANTITY - L_PRE_PERIOD_QTY;
           ---本次还有多少数量需要被下一个单号分摊,就是确定剩下还有多少黑色沙子可用。
           L_CURR_PERIOD_QTY := L_CURR_PERIOD_QTY - L_shipment_consume_Tbl(N).consume_curr_qty;
         ELSE
           ----当黑色沙子数量 小于或者等于 瓶子的可用容量的时候
           -----该瓶子装黑色沙子的数量就是 瓶子的可用容量。
           L_shipment_consume_Tbl(N).consume_curr_qty := L_CURR_PERIOD_QTY;
           -----黑色沙子用完咯!~~一定要赋值0,因为根据黑色沙子的使用情况判断后面是否要退出瓶子的循环。
           L_CURR_PERIOD_QTY := 0;
         END IF;
         L_PRE_PERIOD_QTY:= 0;
       END IF;
        N := N+1;
        ----当黑色沙子用完的时候,要退出循环。因为沙子数量可能很少,但是瓶子很多。。。没必要再循环下去了。
        IF L_CURR_PERIOD_QTY          EXIT;
        END IF;
     END LOOP;
    
     ---显示装的结果。
     FOR I IN 1..L_shipment_consume_Tbl.COUNT LOOP
      DBMS_OUTPUT.PUT_LINE(L_shipment_consume_Tbl(I).SHIPMENT_LINE_ID
      ||'-'|| L_shipment_consume_Tbl(I).PRIMARY_QUANTITY
      ||'-'|| L_shipment_consume_Tbl(I).consume_pre_qty
      ||'-'|| L_shipment_consume_Tbl(I).consume_curr_qty
      );
     
     END LOOP;
END;

/*
---例如:
L_PRE_PERIOD_QTY:0 -L_CURR_PERIOD_QTY:2020.2
18467366-1605.5-0-1605.5
18633076-5014.7-0-414.7

*/
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

See all articles