Search code examples
sqloracle-databaseplsql

Query to create multiple equal records of volume distribution using duration (in terms of months)


Input Output Results

I have a requirement where I need to have records distributed into multiple records based on the duration I get it from a linking table.

Suppose I have a volume of 100 and duration I am getting is 20 months linking table then my output should have 20 records of each 5(100/20).

Could someone please help me with the query how to do this SQL.


Solution

  • The WITH clause is here just to generate some sample data and, as such, it is not a part of the answer.
    You can join the tables ON PRODUCT columns, limit the iterations using LEVEL <= DURATION, group the data and show the amount either as Min, Max or Avg of COST/DURATION rounded to two decimals. I put all of the data in the select list. Here is the complete code with the result.

     WITH
        t_duration AS
            (
                Select 'A' "PRODUCT", 10 "DURATION" From Dual Union All
                Select 'B' "PRODUCT",  6 "DURATION" From Dual Union All
                Select 'C' "PRODUCT",  4 "DURATION" From Dual 
            ),
        t_cost AS
            (
                Select 'A' "PRODUCT", 100 "COST" From Dual Union All
                Select 'B' "PRODUCT",  50 "COST" From Dual Union All
                Select 'C' "PRODUCT",  40 "COST" From Dual 
            )
    
    SELECT
        LEVEL "MONTH_ORDER_NUMBER",
        d.PRODUCT "PRODUCT",
        d.DURATION "DURATION",
        c.COST "COST",
        Round(Avg(c.COST / d.DURATION), 2) "AVG_MONTHLY_AMOUNT",
        Round(Max(c.COST / d.DURATION), 2) "MAX_MONTHLY_AMOUNT",
        Round(Min(c.COST / d.DURATION), 2) "MIN_MONTHLY_AMOUNT"
    FROM
        t_duration d
    INNER JOIN
        t_cost c ON(c.PRODUCT = d.PRODUCT)
    CONNECT BY LEVEL <= d.DURATION
    GROUP BY 
        d.PRODUCT, d.DURATION, c.COST, LEVEL
    ORDER BY 
        d.PRODUCT, LEVEL
    --  
    --  R e s u l t
    --  
    --  MONTH_ORDER_NUMBER PRODUCT   DURATION       COST AVG_MONTHLY_AMOUNT MAX_MONTHLY_AMOUNT MIN_MONTHLY_AMOUNT
    --  ------------------ ------- ---------- ---------- ------------------ ------------------ ------------------
    --                   1 A               10        100                 10                 10                 10 
    --                   2 A               10        100                 10                 10                 10 
    --                   3 A               10        100                 10                 10                 10 
    --                   4 A               10        100                 10                 10                 10 
    --                   5 A               10        100                 10                 10                 10 
    --                   6 A               10        100                 10                 10                 10 
    --                   7 A               10        100                 10                 10                 10 
    --                   8 A               10        100                 10                 10                 10 
    --                   9 A               10        100                 10                 10                 10 
    --                  10 A               10        100                 10                 10                 10 
    --                   1 B                6         50               8.33               8.33               8.33 
    --                   2 B                6         50               8.33               8.33               8.33 
    --                   3 B                6         50               8.33               8.33               8.33 
    --                   4 B                6         50               8.33               8.33               8.33 
    --                   5 B                6         50               8.33               8.33               8.33 
    --                   6 B                6         50               8.33               8.33               8.33 
    --                   1 C                4         40                 10                 10                 10 
    --                   2 C                4         40                 10                 10                 10 
    --                   3 C                4         40                 10                 10                 10 
    --                   4 C                4         40                 10                 10                 10