Analytical function of sum

Hello

I have a SQL query that generates the percentage totals. I can't reproduce this code in the MDB of the repository layer. I created a new logical column, the following sql query:

SELECT id, seq, asset_cost,
CASE
WHEN asset_cost > 0
THEN TURN (RATIO_TO_REPORT)
CASE
WHEN asset_cost > 0
THEN SUM (asset_cost)
(END) OVER (partition BY id) * 100)
Total END
OF the test
GROUP BY id, asset_cost seq

Can anyone help to reproduce the above expression in the column of the logical layer. ]

How can I use the Ratio_to_report function in obiee

The link above shows a workaround solution

Are there alternatives to 'RATIO_TO_REPORT' in OBIEE functions?

Thank you

Published by: sliderrules on May 16, 2012 04:23

Hello

In the formula for the column, give the function that

EVALUATE ('RATIO_TO_REPORT (%1 ON (2% PARTITION)')) DOUBLE, "SALES agreement". "' Cost of the asset", "Details of the agreement. ("" Contract number ")

The 1%, 2% would get substituted with corresponding columns by the Server BI himself.

I hope this helps.

Thank you
Diakité

Tags: Business Intelligence

Similar Questions

  • Truncate output of analytical function?

    For example this query:

    Select month, sum (tot_sales) monthly_sales,.

    AVG (Sum (tot_sales)) (any order by month

    between 1 above and 1 below) rolling_avg

    orders

    where year = 2001 and region_id = 6

    Group by month;

    gives me an output which includes several decimal places for the rolling_avg column.

    Is there a way to truncate this? I tried to use the rounded outside the analytical function and surely enough, it didn't work. I can't think otherwise.

    You can use an external selection on the result of this query

    select trunc(rolling_avg) from
    ( rolling_avg query);
    
  • Analytical function?

    Hello

    I have two tables b rates and transactions (b).

    For each b.FS, I want to know new amount (b.amt time a.rate to the corresponding line of FS b and if a.na is valid based on the flag of the exclusion and the interval defined in the table's).

    It is much easier to explain with an example.

    So here we go...

    Exclude_flag = E (exclude): for b.fs = 433638, b.na = 80000. I have 2 lines in the table for this fs.  Both have E exclude_flag (exclude).  I want to go on all the lines in this FS in the table in a query and I return only one row in the result, if and only if b.na falls out of scope of a.na_min_value and a.na_max_values.  In this example, it falls outside the range excluded for both lines (first two lines of the table a).

    Similarly, exclude_flag = I (Include). for b.fs = 432828, b.na = 17200. I have 2 lines in the table for this fs.  Both have an exclude_flag of I (include).  I want to go on all the lines in this FS in the table in a query and I return only one row in the result, if and only if b.na is between a.na_min_value and a.na_max_values.  In this example, it falls in the range of both the range include for the two lines (line 6 and 7 of the table a).

    The following query gives me two lines for each b.fs.  It is possible to get what I'm looking for simply using sql (possibly write the analytical function?) or I have to write the pl/sql routine for this?

    WITH rates_table

    Did YOU (select ' E' include_exclude_flag, "81000 ' na_min_value, na_max_value '81999', '433638' FS, 0.8 of double rate

    UNION

    Select 'E' include_exclude_flag, na_min_value '84000', na_max_value '84999', '433638' FS, 0.8 of double rate

    UNION

    Select 'I' include_exclude_flag, na_min_value '12000', na_max_value '12999', '432828' FS, rate 0.25 double

    UNION

    Select 'I' include_exclude_flag, na_min_value '13000', na_max_value '13999', '432828' FS, rate 0.25 double

    UNION

    Select 'I' include_exclude_flag, na_min_value '15000', na_max_value '15000', '432828' FS, rate 0.25 double

    UNION

    Select 'I' include_exclude_flag, na_min_value '16100', na_max_value '18000', '432828' FS, rate 0.25 double

    UNION

    Select 'I' include_exclude_flag, '17100' na_min_value, na_max_value '18000', '432828' FS, rate 0.25 double

    UNION

    Select 'I' include_exclude_flag, na_min_value '02440', na_max_value '02470', '016532' FS, 0.35 rate double

    UNION

    Select 'E' include_exclude_flag, na_min_value ' 21000 ', '21000' na_max_value, ' 200020' FS, 0.35 double rate).

    transaction_table AS

    (select '433638' FS '80000' NA, 300 double amt)

    Union

    Select '432828' FS '17200' NA, amt 500 double

    )

    Select * from rates_table a, transaction_table b

    where 1 = 1

    and ((b.na PAS entre a.na_min_value et a.na_max_value et a.include_exclude_flag = «E») GOLD ())

    b.na between a.na_min_value and a.na_max_value and a.include_exclude_flag = 'I'))

    and b.fs = a.fs

    ;

    Any help is greatly appreciated.  I use oracle 11i

    @OP,

    For this kind of problems, we get the number of rows that satisfy the required conditions or who do not meet the required conditions. And then eliminate the line that should be eliminated.

    As below, I calculate four counts (number of join lines)

    EOBCNT - excluded and Out of Bound

    EIBCNT - excluded and in the limit

    IOBCNT - included and Out of Bound

    IIBCNT - included and within the limits

    Once those are calculated, simply return the lines that have EIBCNT and IOBCNT are zero.

    For Ex:

    > WITH

    rates_table

    AS LONG AS)

    Select 'E' include_exclude_flag, na_min_value '79999', na_max_value '79999', '433638' FS, 0.8 double UNION rates

    -Select 'E' include_exclude_flag, na_min_value ' 79999', na_max_value '80000', '433638' FS, 0.8 double UNION rate - TEST - THE

    -If you uncomment the last line and then 433638 will not be returned

    Select 'E' include_exclude_flag, "81000 ' na_min_value, na_max_value '81999', '433638' FS, 0.8 double UNION rates

    Select 'E' include_exclude_flag, na_min_value '84000', na_max_value '84999', '433638' FS, 0.8 double UNION rates

    Select 'I' include_exclude_flag, na_min_value '12000', na_max_value '12999', '432828' FS, 0.25 rate double UNION

    Select 'I' include_exclude_flag, na_min_value '13000', na_max_value '13999', '432828' FS, 0.25 rate double UNION

    Select 'I' include_exclude_flag, na_min_value '15000', na_max_value '15000', '432828' FS, 0.25 rate double UNION

    Select 'I' include_exclude_flag, na_min_value '16100', na_max_value '18000', '432828' FS, 0.25 rate double UNION

    Select 'I' include_exclude_flag, '17100' na_min_value, na_max_value '18000', '432828' FS, 0.25 rate double UNION

    Select 'I' include_exclude_flag, na_min_value '02440', na_max_value '02470', '016532' FS, 0.35 rate double UNION

    Select 'E' include_exclude_flag, na_min_value ' 21000 ', '21000' na_max_value, ' 200020' FS, 0.35 double rate).

    transaction_table AS

    (select '433638' FS '80000' NA, amt 300 Union double

    Select '432828' FS '17200' NA, amt 500 double

    )

    getcnts as)

    SELECT a.*

    b.na, b.amt

    , sum (case when (b.na NOT BETWEEN a.na_min_value)

    AND a.na_max_value

    AND a.include_exclude_flag = 'E '.

    end) then 1 else 0) on (a.fs partition) Eobcnt

    , sum (case when (b.na NOT BETWEEN a.na_min_value)

    AND a.na_max_value

    AND a.include_exclude_flag = 'I '.

    end) then 1 else 0) on (a.fs partition) Iobcnt

    , sum (case when (b.na BETWEEN a.na_min_value)

    AND a.na_max_value

    AND a.include_exclude_flag = 'E '.

    end) then 1 else 0) on (a.fs partition) Eibcnt

    , sum (case when (b.na BETWEEN a.na_min_value)

    AND a.na_max_value

    AND a.include_exclude_flag = 'I '.

    end) then 1 else 0) on (a.fs partition) Iibcnt

    OF rates_table one

    transaction_table b

    WHERE b.fs = a.fs

    )

    getrows as)

    Select x.*, row_number() on

    (partition by order of fs with null desc) getcnts rn x

    where IOBCNT = 0 and eibcnt = 0 - remove lines that are not needed (IF and ONLY if)

    )

    Select * from getrows

    where rn = 1 - to limit a SINGLE row (arbitrarily)

    INCLUDE_EXCLUDE_FLAG NA_MIN_VALUE NA_MAX_VALUE FS RATE NA AMT EOBCNT IOBCNT EIBCNT IIBCNT RN

    -------------------- ------------ ------------ ------ ---------- ----- ---------- ---------- ---------- ---------- ---------- ----------

    433638 84999 84000.8 E 80000 300 3 0 0 0 1

    I hope this helps.

  • Is there a shorter way (better) with analytical functions?

    Here's a little test scenario:
    create table t 
    ( id   number,
      pos  number,
      typ  number,
      m    number);
      
    insert into t values (1,1,1,100);
    insert into t values (1,2,1,100);
    insert into t values (1,3,2, 50);
    insert into t values (2,1,3, 30);
    insert into t values (2,2,4, 70);
    insert into t values (3,1,1,100);
    insert into t values (3,2,2, 50);
    insert into t values (4,1,3, 30);
    insert into t values (4,2,5, 80);
    insert into t values (4,3,3, 30);
    insert into t values (5,1,3, 30);
    insert into t values (5,2,6, 30);
    insert into t values (6,1,2, 50);
    insert into t values (6,2,7, 50);
    insert into t values (6,3,2, 50);
    insert into t values (7,1,4, 70);
    insert into t values (7,2,4, 70);
    insert into t values (7,3,4, 70);
    For each id, I want to add all the values of m only when they have a different type. It would be a long journey:
    with t1 as
      (select 
         id, 
         typ, 
         min(m) m1 
       from t
       group by id, typ)
    select
      id,
      sum(m1) f
    from t1
    group by id
    order by 1;
    
            ID          F
    ---------- ----------
             1        150 
             2        100 
             3        150 
             4        110 
             5         60 
             6        100 
             7         70 
    but I wonder, is it possible to get this result with a single statement select using analytic functions, something like
    select 
      id, 
      sum(m) over (partition by distinct typ) F    -- this does not work. It's only an idea how it might look like
    from t
    group by id;

    This is firstly a collection with the id, type with calculation of the min for each id, type the combination.
    By subsequently for each id of the sum of the minutes (for each combination of id, type for this particular id) is summarized.

    select distinct
     id, sum(min(m)) over (partition by id)
    from data
    group by id, typ
    order by id
    

    Published by: chris227 on 15.03.2013 07:39

  • Purpose of the ORDER BY clause in the analytic function Min Max

    I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.

    user10566312 wrote:
    I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.

    It is a good point that many developers are not so aware. As far as I understand it the way it works.

    Some analytical functions do not need an order by or windowing clause (SUM, COUNT, MIN, etc.). If there is no specified window, then the full score is the window.
    As soon as you add a command also add you a windowing clause. This window has the default value of 'rank ofrows between unbounded preceding and current_row. So as soon as you add an order by clause, you get a sliding window.

    Documentation: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm

    windowing_clause
    ...
    You cannot specify this clause unless you specified the order_by_clause. Window limits defined by the clause RANGE you can not specify only a single expression to the > order_by_clause. Please refer to 'Restrictions on the ORDER BY Clause'.

    example of

    with testdata as (select 10 numval, level lv from dual connect by level < 10)
    select lv, numval, sum(numval) over () sum1, sum(numval) over (order by lv) sum2
    from testdata;
    
    LV NUMVAL SUM1 SUM2
    -- ------ ---- ----
     1     10   90   10
     2     10   90   20
     3     10   90   30
     4     10   90   40
     5     10   90   50
     6     10   90   60
     7     10   90   70
     8     10   90   80
     9     10   90   90 
    

    Published by: Sven w. on 25 Sep 2012 16:57 - default behavior has been corrected. Thanks to Chris

  • problem of analytic function

    Hello

    I have a problem using the analytical function: when I run this query

    SELECT TSIUPSITE, TSIUPCEAN, TSIUPDATE, sum (TSIUPCA) TSIUPCA, TSIUPCTVA, TSIUPP4N, TSIUPPIEC,.
    Sum (TSIUPQTE) TSIUPQTE, sum (TSIUPQTEP) TSIUPQTEP, TSIUPMDIU, TSIUPMDar,
    Sum (TSIUPCRIU) TSIUPCRIU, sum (TSIUPCRAR) TSIUPCRAR, trunc (TSIUPDCRE) TSIUPDCRE, trunc (TSIUPDMAJ) TSIUPDMAJ,
    TSIUPUTIL, TSIUPTRT, TSIUPNERR, TSIUPMESS.
    TSIUPTMVT, TSIUPSMAN, TSIUPMOTIF, sum (TSIUPMHT) TSIUPMHT, 0 vtanfisc.
    TSIUPDATEVERIF, TSIUPNSEQ, TSIUPCINV, count (*) over (partition TSIUPSITE, TSIUPCEAN, TSIUP_TRT) CONTA_ARTICOLO
    OF TST_FLIISR_VTEREMART
    WHERE 1 = 1 - TSIUP_TRT = 1
    AND TSIUPDATE = to_date('27082012','ddmmyyyy')
    and TSIUP_NTRX = 172
    AND TSIUPSITE = 10025
    AND TSIUPCEAN = '8012452018825'
    GROUP OF TSIUPSITE, TSIUPCEAN, TSIUPDATE, TSIUPCTVA, TSIUPP4N, TSIUPPIEC,
    TSIUPMDIU, TSIUPMDar, trunc (TSIUPDCRE), trunc (TSIUPDMAJ), TSIUPUTIL, TSIUPTRT, TSIUPNERR, TSIUPMESS,
    TSIUPTMVT, TSIUPSMAN, TSIUPMOTIF, 0,
    TSIUPDATEVERIF, TSIUPNSEQ, TSIUPCINV
    ORDER OF TSIUPSITE, TSIUPDATE;

    I have the error ORA-00979: not an expression GROUP BY related to the area of TSIUP_TRT, infact, if I run it

    SELECT TSIUPSITE, TSIUPCEAN, TSIUPDATE, sum (TSIUPCA) TSIUPCA, TSIUPCTVA, TSIUPP4N, TSIUPPIEC,.
    Sum (TSIUPQTE) TSIUPQTE, sum (TSIUPQTEP) TSIUPQTEP, TSIUPMDIU, TSIUPMDar,
    Sum (TSIUPCRIU) TSIUPCRIU, sum (TSIUPCRAR) TSIUPCRAR, trunc (TSIUPDCRE) TSIUPDCRE, trunc (TSIUPDMAJ) TSIUPDMAJ,
    TSIUPUTIL, TSIUPTRT, TSIUPNERR, TSIUPMESS.
    TSIUPTMVT, TSIUPSMAN, TSIUPMOTIF, sum (TSIUPMHT) TSIUPMHT, 0 vtanfisc.
    TSIUPDATEVERIF, TSIUPNSEQ, TSIUPCINV, count (*) over (partition TSIUPSITE, TSIUPCEAN) CONTA_ARTICOLO
    OF TST_FLIISR_VTEREMART
    WHERE 1 = 1 - TSIUP_TRT = 1
    AND TSIUPDATE = to_date('27082012','ddmmyyyy')
    and TSIUP_NTRX = 172
    AND TSIUPSITE = 10025
    AND TSIUPCEAN = '8012452018825'
    GROUP OF TSIUPSITE, TSIUPCEAN, TSIUPDATE, TSIUPCTVA, TSIUPP4N, TSIUPPIEC,
    TSIUPMDIU, TSIUPMDar, trunc (TSIUPDCRE), trunc (TSIUPDMAJ), TSIUPUTIL, TSIUPTRT, TSIUPNERR, TSIUPMESS,
    TSIUPTMVT, TSIUPSMAN, TSIUPMOTIF, 0,
    TSIUPDATEVERIF, TSIUPNSEQ, TSIUPCINV
    ORDER OF TSIUPSITE, TSIUPDATE;

    I have no problem. Now the difference between the TSIUPCEAN (or TSIUPSITE) and TSIUP_TRT that TSIUP_TRT is not in the Group By clause, but, to be honest, I don't know why I have this problem using using an analytic function.

    Thanks for help

    Hello

    I think that you are not analytic function correctly.

    Analytical functions will be run for each line. Where as Group BY will run for groups of data.

    See example below for you reference.

    Example 1:
    
    -- Below query displays number of employees for each department. Since we have used analytical function for each row you are getting the number of employees based on the department id.
    
    SQL> SELECT e.department_id,count(*) OVER (PARTITION BY e.department_id) cnt_analytic
      2  FROM employees e
      3  WHERE e.department_id IN (10,20,30);
    
    DEPARTMENT_ID CNT_ANALYTIC
    ------------- ------------
               10            1
               20            2
               20            2
               30            6
               30            6
               30            6
               30            6
               30            6
               30            6
    
    9 rows selected.
    
    Example 2:
    
    -- Since I have used GROUP BY clause I'm getting only single row for each department.
    
    SQL> SELECT e.department_id, count(*) cnt_group
      2  FROM employees e
      3  WHERE e.department_id IN (10,20,30)
      4  GROUP BY e.department_id;
    
    DEPARTMENT_ID  CNT_GROUP
    ------------- ----------
               10          1
               20          2
               30          6
    

    Finally, what I'm trying to explain is - if you use the analytical function with the GROUP BY clause, the query will not give the ful menaing result set.

    See below

    SQL> SELECT e.department_id,count(*) OVER (PARTITION BY e.department_id) cnt_analytic, count(*) cnt_grp
      2  FROM employees e
      3  WHERE e.department_id IN (10,20,30)
      4  GROUP BY e.department_id;
    
    DEPARTMENT_ID CNT_ANALYTIC    CNT_GRP
    ------------- ------------ ----------
               10            1          1
               20            1          2
               30            1          6
    
  • Problem with analytical function for date

    Hi all

    ORCL worm:
    Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production."
    AMT for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    I have a problem with the analtical for the date function. I'm trying to group records based on timestamp, but I'm failing to do.
    Could you please help me find where I'm missing.
    This is the subquery. No issue with this. I'm just posting it for reference. 
    select sum(disclosed_cost_allocation.to_be_paid_amt) amt,
        substr(reference_data.ref_code,4,10) cd,
        to_char(external_order_status.status_updated_tmstp, 'DD-MON-YYYY HH24:MI:SS') tmstp,
        DISCLOSED_CLOSING_COST.DISCLOSED_CLOSING_COST_ID id
      FROM Deal.Fee_Mapping_Definition ,
        Deal.Fee_Index_Definition ,
        Deal.Fee_Closing_Cost_Item,
        Deal.Closing_Cost,
        Deal.Document_Generation_Request,
        deal.PRODUCT_REQUEST,
        deal.External_Order_Request,
        deal.External_Order_Status,
        deal. DISCLOSED_CLOSING_COST,
        deal.DISCLOSED_COST_ALLOCATION,
        deal.reference_data
      WHERE Fee_Mapping_Definition.Fee_Code                    = Fee_Index_Definition.Fee_Code
      AND Fee_Index_Definition.Fee_Index_Definition_Id         = Fee_Closing_Cost_Item.Fee_Index_Definition_Id
      AND Fee_Closing_Cost_Item.Closing_Cost_Id                = Closing_Cost.Closing_Cost_Id
      AND CLOSING_COST.PRODUCT_REQUEST_ID                      = Document_Generation_Request.Product_Request_Id
      AND closing_cost.product_request_id                      = product_request.product_request_id
      AND Product_Request.Deal_Id                              = External_Order_Request.Deal_Id
      AND external_order_request.external_order_request_id     = external_order_status.external_order_request_id
      AND external_order_request.external_order_request_id     = disclosed_closing_cost.external_order_request_id
      AND DISCLOSED_CLOSING_COST. DISCLOSED_CLOSING_COST_ID    = DISCLOSED_COST_ALLOCATION.DISCLOSED_CLOSING_COST_ID
      AND Fee_Index_Definition.Fee_Index_Definition_Id         = Disclosed_Closing_Cost.Fee_Index_Definition_Id
      AND Fee_Mapping_Definition.Document_Line_Series_Ref_Id   = Reference_Data.Reference_Data_Id
      AND Document_Generation_Request.Document_Package_Ref_Id IN (7392 ,2209 )
      AND External_Order_Status.Order_Status_Txt               = ('GenerationCompleted')
      AND Fee_Mapping_Definition.Document_Line_Series_Ref_Id  IN ( 7789, 7788,7596 )
      AND FEE_MAPPING_DEFINITION.DOCUMENT_TYPE_REF_ID          = 1099
      AND Document_Generation_Request.Product_Request_Id      IN
        (SELECT PRODUCT_REQUEST.PRODUCT_REQUEST_id
        FROM Deal.Disclosed_Cost_Allocation,
          Deal.Disclosed_Closing_Cost,
          DEAL.External_Order_Request,
          DEAL.PRODUCT_REQUEST,
          Deal.Scenario
        WHERE Disclosed_Cost_Allocation.Disclosed_Closing_Cost_Id = Disclosed_Closing_Cost.Disclosed_Closing_Cost_Id
        AND Disclosed_Closing_Cost.External_Order_Request_Id      = External_Order_Request.External_Order_Request_Id
        AND External_Order_Request.Deal_Id                        = Product_Request.Deal_Id
        AND product_request.scenario_id                           = scenario.scenario_id
        AND SCENARIO.SCENARIO_STATUS_TYPE_REF_ID                  = 7206
        AND product_request.servicing_loan_acct_num              IS NOT NULL
        AND product_request.servicing_loan_acct_num               = 0017498379
          --AND Disclosed_Cost_Allocation.Disclosed_Cost_Allocation_Id = 5095263
        )
      GROUP BY DISCLOSED_CLOSING_COST.DISCLOSED_CLOSING_COST_ID,
        External_Order_Status.Status_Updated_Tmstp,
        Reference_Data.Ref_Code,
        disclosed_cost_allocation.to_be_paid_amt
      order by 3 desc,
        1 DESC;
    
    Result:
    2000     1304-1399     28-JUL-2012 19:49:47     6880959
    312     1302     28-JUL-2012 19:49:47     6880958
    76     1303     28-JUL-2012 19:49:47     6880957
    2000     1304-1399     28-JUL-2012 18:02:16     6880539
    312     1302     28-JUL-2012 18:02:16     6880538
    76     1303     28-JUL-2012 18:02:16     6880537
    
    
    But, when I try to group the timestamp using analytical function,
    
    
    select amt 
            ,cd 
            ,rank() over(partition by tmstp order by tmstp desc) rn 
    from 
    (select sum(disclosed_cost_allocation.to_be_paid_amt) amt,
        substr(reference_data.ref_code,4,10) cd,
        to_char(external_order_status.status_updated_tmstp, 'DD-MON-YYYY HH24:MI:SS') tmstp,
        DISCLOSED_CLOSING_COST.DISCLOSED_CLOSING_COST_ID id
      FROM Deal.Fee_Mapping_Definition ,
        Deal.Fee_Index_Definition ,
        Deal.Fee_Closing_Cost_Item,
        Deal.Closing_Cost,
        Deal.Document_Generation_Request,
        deal.PRODUCT_REQUEST,
        deal.External_Order_Request,
        deal.External_Order_Status,
        deal. DISCLOSED_CLOSING_COST,
        deal.DISCLOSED_COST_ALLOCATION,
        deal.reference_data
      WHERE Fee_Mapping_Definition.Fee_Code                    = Fee_Index_Definition.Fee_Code
      AND Fee_Index_Definition.Fee_Index_Definition_Id         = Fee_Closing_Cost_Item.Fee_Index_Definition_Id
      AND Fee_Closing_Cost_Item.Closing_Cost_Id                = Closing_Cost.Closing_Cost_Id
      AND CLOSING_COST.PRODUCT_REQUEST_ID                      = Document_Generation_Request.Product_Request_Id
      AND closing_cost.product_request_id                      = product_request.product_request_id
      AND Product_Request.Deal_Id                              = External_Order_Request.Deal_Id
      AND external_order_request.external_order_request_id     = external_order_status.external_order_request_id
      AND external_order_request.external_order_request_id     = disclosed_closing_cost.external_order_request_id
      AND DISCLOSED_CLOSING_COST. DISCLOSED_CLOSING_COST_ID    = DISCLOSED_COST_ALLOCATION.DISCLOSED_CLOSING_COST_ID
      AND Fee_Index_Definition.Fee_Index_Definition_Id         = Disclosed_Closing_Cost.Fee_Index_Definition_Id
      AND Fee_Mapping_Definition.Document_Line_Series_Ref_Id   = Reference_Data.Reference_Data_Id
      AND Document_Generation_Request.Document_Package_Ref_Id IN (7392 ,2209 )
      AND External_Order_Status.Order_Status_Txt               = ('GenerationCompleted')
      AND Fee_Mapping_Definition.Document_Line_Series_Ref_Id  IN ( 7789, 7788,7596 )
      AND FEE_MAPPING_DEFINITION.DOCUMENT_TYPE_REF_ID          = 1099
      AND Document_Generation_Request.Product_Request_Id      IN
        (SELECT PRODUCT_REQUEST.PRODUCT_REQUEST_id
        FROM Deal.Disclosed_Cost_Allocation,
          Deal.Disclosed_Closing_Cost,
          DEAL.External_Order_Request,
          DEAL.PRODUCT_REQUEST,
          Deal.Scenario
        WHERE Disclosed_Cost_Allocation.Disclosed_Closing_Cost_Id = Disclosed_Closing_Cost.Disclosed_Closing_Cost_Id
        AND Disclosed_Closing_Cost.External_Order_Request_Id      = External_Order_Request.External_Order_Request_Id
        AND External_Order_Request.Deal_Id                        = Product_Request.Deal_Id
        AND product_request.scenario_id                           = scenario.scenario_id
        AND SCENARIO.SCENARIO_STATUS_TYPE_REF_ID                  = 7206
        AND product_request.servicing_loan_acct_num              IS NOT NULL
        AND product_request.servicing_loan_acct_num               = 0017498379
          --AND Disclosed_Cost_Allocation.Disclosed_Cost_Allocation_Id = 5095263
        )
      GROUP BY DISCLOSED_CLOSING_COST.DISCLOSED_CLOSING_COST_ID,
        External_Order_Status.Status_Updated_Tmstp,
        Reference_Data.Ref_Code,
        disclosed_cost_allocation.to_be_paid_amt
      order by 3 desc,
        1 DESC);
    
    Result:
    312     1302            1
    2000     1304-1399     1
    76     1303            1
    312     1302            1
    2000     1304-1399     1
    76     1303            1 
    
    
    Required output:
    312     1302            1
    2000     1304-1399     1
    76     1303            1
    312     1302            2
    2000     1304-1399     2
    76     1303            2
    THX
    Rod.

    Hey, Rod,

    My guess is that you want:

    , dense_rank () over (order by  tmstp  desc)  AS rn 
    

    RANK means you'll jump numbers when there is a link. For example, if all 3 rows have the exact same last tmstp, all 3 rows would be assigned number 1, GRADE would assign 4 to the next line, but DENSE_RANK attributes 2.

    "PARTITION x" means that you are looking for a separate series of numbers (starting with 1) for each value of x. If you want just a series of numbers for the entire result set, then do not use a PARTITION BY clause at all. (PARTITION BY is never required.)
    Maybe you want to PARTITIONNER IN cd. I can't do it without some examples of data, as well as an explanation of why you want the results of these data.
    You certainly don't want to PARTITION you BY the same expression ORDER BY; It simply means that all the lines are tied for #1.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Simplify the problem as much as possible.
    Always tell what version of Oracle you are using.
    See the FAQ forum {message identifier: = 9360002}

    Published by: Frank Kulash, August 1, 2012 13:20

  • May result by analytic function that follows

    Hi all

    I am currently using oracle 10g.

    create table
    CREATE TABLE fortest
    (  PROD             VARCHAR2(40 BYTE),
      prodvalues     number);
    INSERT statement
    insert into fortest values ('dental',10)
       insert into fortest values ('dental',4)
        insert into fortest values ('dental',13)
         insert into fortest values ('dental',3)
          insert into fortest values ('vision',2)
           insert into fortest values ('vision',11)
           insert into fortest values ('vision',33) 
            insert into fortest values ('vision',7)
    I need the output as follows
    prod        prodvalues <5         prodvalues >=5 and less than 10               prodvalues >=10
    dental         2                                     0                                      2
    vision         1                                     1                                      2
    first column should give me separate prod, prodvalues5 column: should give me the number of similar prod with prodvalues less than 5, same thirdcolumn should have County of similar prod with prodvalues > = 5 and prodvalue < 10 and so on.

    Please, not the names of columns of the output tables are just for reference, and I will not use them.


    Thanks in advance.

    Hi Bob,

    You don't have to use analytical functions. Here's a solution that doesn't use analytical functions:

    with temp as (select prod, case
                               when prodvalues <=5 then 1
                               else                    0
                               end  as prod5,
                               case
                               when prodvalues >=5 and prodvalues <10 then 1
                               else                    0
                               end  as prod5_10,
                               case
                               when prodvalues >= 10 then 1
                               else                    0
                               end  as prod10
                               from fortest)
                               select prod,sum(prod5) as prod5 ,  sum(prod5_10) as prod5_10 ,sum(prod10) as prod10
                               from temp
                               group by prod
    
  • Try to use a function of Group on an analytic function

    Hello

    We use the Oracle 11.1.

    I have an analytic function that gives me the DENSE_RANK of lines groups. I would like to return the highest value of each group.

    The function looks like:
    DENSE_RANK( ) OVER (PARTITION BY METRIC ORDER BY sum(AEROBIC_EXERCISE_P1), COACHID)
    When I try to use:
    MAX(DENSE_RANK( ) OVER (PARTITION BY METRIC ORDER BY sum(AEROBIC_EXERCISE_P1), COACHID))
    I have get and error
    ORA-30483: window  functions are not allowed here
    Is it possible that I can do this?

    Hello

    Sorry, my mistake. You do not have an ORDER BY clause analytic it.

    COUNT (DISTINCT SUM (aerobic_exercise_p1)) OVER (PARTITION BY  metric)
    

    If you would post CREATE TABLE and INSERT statements, then I could test it.

  • by using the analytical function to get the right output.

    Hello all;

    I have the following date of sample below
    create table temp_one
    (
           id number(30),   
          placeid varchar2(400),
          issuedate  date,
          person varchar2(400),
          failures number(30),
          primary key(id)
    );
    
    insert into temp_one values (1, 'NY', to_date('03/04/2011', 'MM/DD/YYYY'), 'John', 3);
    
    insert into temp_one values (2, 'NY', to_date('03/03/2011', 'MM/DD/YYYY'), 'Adam', 7);
    
    insert into temp_one values (3, 'Mexico', to_date('03/04/2011', 'MM/DD/YYYY'), 'Wendy', 3);
    
    insert into temp_one values (4, 'Mexico', to_date('03/14/2011', 'MM/DD/YYYY'), 'Gerry', 3);
    
    insert into temp_one values (5, 'Mexico', to_date('03/15/2011', 'MM/DD/YYYY'), 'Zick', 9);
    
    insert into temp_one values (6, 'London', to_date('03/16/2011', 'MM/DD/YYYY'), 'Mike', 8);
    It's the output I want
    placeid       issueperiod                               failures
    NY              02/28/2011 - 03/06/2011          10
    Mexico       02/28/2011 - 03/06/2011           3
    Mexico        03/14/2011 - 03/20/2011          12
    London        03/14/2011 - 03/20/2011          8
    Any help is appreciated. I'll post my request as soon as I can think of a good logic for this...

    Hello

    user13328581 wrote:
    ... Please note, I'm still learning how to use analytical functions.

    It doesn't matter; analytical functions will not help in this problem. The SUM aggregate function is all you need.
    But what do you need to GROUP BY? What is the value of each row of the result will represent? A placeid? Yes, each line will represent only placedid, but it will be divided further. You want a separate line of the output for each placeid and every week, then you'll want of the week and GROUP BY placeid. You don't want to GROUP BY the raw issuedate; that would put on 3 March and 4 March in separate groups. And you don't want to GROUP BY failures; This would mean that a line with 3 failures could never be in the same group in line with 9 failures.

    This becomes the output you posted from the sample data you posted:

    SELECT       placeid
    ,             TO_CHAR ( TRUNC (issuedate, 'IW')
                  , 'MM/DD/YYYY'
                ) || ' - '|| TO_CHAR ( TRUNC (issuedate, 'IW') + 6
                                             , 'MM/DD/YYY'
                               )     AS issueperiod
    ,       SUM (failures)                  AS sumfailures
    FROM        temp_one
    GROUP BY  placeid
    ,            TRUNC (issuedate, 'IW')
    ;
    

    You can use a subquery to calculate TRUNC (issuedate, 'IW') once. The code would be of about as complicated, efficiency probably will not improve substantially and the results would be the same.

  • With the help of analytical functions

    Hi all

    I'm using ODI 11 g (11.1.1.3.0) and I'm doing an interface using analytical functions in the column map, something like below.
    Salary on (partition of...)

    The problem is that when ODI saw the sum he considers this an aggregate function and the group. Is it possible to understand that it is not an aggregate in ODI function?


    I tried to create an option to specify whether it is analytic, then updated IKM with no luck.
    < % if (odiRef.getUserExit("ANALYTIC").equals("1")) {% >}
    < %} else {% >}
    < % = odiRef.getGrpBy (i) % >
    < % = odiRef.getHaving (i) % >
    < %} % >

    Thanks in advance

    Seth,

    Try this thing posted by Uli:
    http://www.business-intelligence-quotient.com/?p=905

  • date ranges - possible to use analytical functions?

    The following datastructure must be converted into a daterange datastructure.
    START_DATE END_DATE      AMMOUNT
    ---------- ---------- ----------
    01-01-2010 28-02-2010         10
    01-02-2010 31-03-2010         20
    01-03-2010 31-05-2010         30
    01-09-2010 31-12-2010         40
    Working solution:
    with date_ranges
    as   ( select to_date('01-01-2010','dd-mm-yyyy') start_date
           ,      to_date('28-02-2010','dd-mm-yyyy') end_date
           ,      10                                 ammount
           from   dual
           union all
           select to_date('01-02-2010','dd-mm-yyyy') start_date
           ,      to_date('31-03-2010','dd-mm-yyyy') end_date
           ,      20                                 ammount
           from   dual
           union all
           select to_date('01-03-2010','dd-mm-yyyy') start_date
           ,      to_date('31-05-2010','dd-mm-yyyy') end_date
           ,      30                                 ammount
           from   dual
           union all
           select to_date('01-09-2010','dd-mm-yyyy') start_date
           ,      to_date('31-12-2010','dd-mm-yyyy') end_date
           ,      40                                 ammount
           from   dual
          )
    select   rne.start_date
    ,        lead (rne.start_date-1,1)  over (order by rne.start_date) end_date
    ,        ( select sum(dre2.ammount)
               from   date_ranges dre2
               where  rne.start_date >= dre2.start_date
               and    rne.start_date <= dre2.end_date
             ) range_ammount
    from     ( select dre.start_date
               from   date_ranges dre
               union -- implicit distinct
               select dre.end_date + 1
               from   date_ranges dre
             ) rne
    order by rne.start_date
    /
    Output:
    START_DATE END_DATE   RANGE_AMMOUNT
    ---------- ---------- -------------
    01-01-2010 31-01-2010            10
    01-02-2010 28-02-2010            30
    01-03-2010 31-03-2010            50
    01-04-2010 31-05-2010            30
    01-06-2010 31-08-2010
    01-09-2010 31-12-2010            40
    01-01-2011
    
    7 rows selected.
    However, I would like to use an analytical function to calculate the range_ammount. Is this possible?

    Published by: user5909557 on July 29, 2010 06:19

    Hello

    Welcome to the forum!

    Yes, you can replace the scalar sub-queriy with a SUMMARY, like this:

    WITH  change_data   AS
    (
         SELECT     start_date     AS change_date
         ,     ammount          AS net_amount
         FROM     date_ranges
              --
        UNION
              --
         SELECT     end_date + 1     AS change_date
         ,     -ammount        AS net_amount
         FROM     date_ranges
    )
    ,     got_range_amount     AS
    (
         SELECT     change_date          AS start_date
         ,     LEAD (change_date) OVER (ORDER BY  change_date) - 1
                                     AS end_date
         ,     SUM (net_amount)   OVER (ORDER BY  change_date)
                                    AS range_amount
         FROM    change_data
    )
    ,     got_grp          AS
    (
         SELECT     start_date
         ,     end_date
         ,     range_amount
         ,     ROW_NUMBER () OVER ( ORDER BY        start_date, end_date)
               - ROW_NUMBER () OVER ( PARTITION BY  range_amount
                                         ORDER BY          start_date, end_date
                           )         AS grp
         FROM    got_range_amount
    )
    SELECT       MIN (start_date)     AS start_date
    ,       MAX (end_date)     AS end_date
    ,       range_amount
    FROM       got_grp
    GROUP BY  grp
    ,            range_amount
    ORDER BY  grp
    ;
    

    This should be much more effective.

    The code is longer than what you posted. It is largely because it includes consecutive groups with the same amount.
    For example, if you add this line the sample data:

    --
           union all
           select to_date('02-01-2010','dd-mm-yyyy') start_date
           ,      to_date('30-12-2010','dd-mm-yyyy') end_date
           ,      0                                 ammount
           from   dual
    

    The query that you posted the product:

    START_DAT END_DATE  RANGE_AMMOUNT
    --------- --------- -------------
    01-JAN-10 01-JAN-10            10
    02-JAN-10 31-JAN-10            10
    01-FEB-10 28-FEB-10            30
    01-MAR-10 31-MAR-10            50
    01-APR-10 31-MAY-10            30
    01-JUN-10 31-AUG-10             0
    01-SEP-10 30-DEC-10            40
    31-DEC-10 31-DEC-10            40
    01-JAN-11
    

    I suppose you want only a new production line where the changes of range_amount., it is:

    START_DAT END_DATE  RANGE_AMOUNT
    --------- --------- ------------
    01-JAN-10 31-JAN-10           10
    01-FEB-10 28-FEB-10           30
    01-MAR-10 31-MAR-10           50
    01-APR-10 31-MAY-10           30
    01-JUN-10 31-AUG-10            0
    01-SEP-10 31-DEC-10           40
    01-JAN-11                      0
    

    Of course, you can change the original query so that it did, but it would eventually just as complex as the above query, but less effective.
    Conversely, if you prefer the longer output, then you need not got_grp Tahina-query in the above query.

    Thanks for posting the CREATE TABLE and INSERT statements; It is very useful.
    There are people who use this forum for years and have yet to be begged to do.

  • Help me on the analytic function

    Hello

    I use oracle version
    SQL> select * From v$version;
    
    BANNER
    ----------------------------------------------------------------
    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    PL/SQL Release 9.2.0.8.0 - Production
    CORE    9.2.0.8.0       Production
    TNS for Solaris: Version 9.2.0.8.0 - Production
    NLSRTL Version 9.2.0.8.0 - Production
    I have a following tables
    CREATE TABLE emp_info(emp_id number,ename varchar2(10),chk_date date);
    
    CREATE TABLE emp_month(emp_id number,emp_month_date date,chk_amt number(10,2));
    
    insert into emp_info VALUES (101,'PAUL',to_date('01-MAR-2009','DD-MON-YYYY'));
    insert into emp_info VALUES (102,'JOHN',to_date('01-APR-2009','DD-MON-YYYY'));
    insert into emp_info VALUES (103,'KRIS',to_date('01-MAY-2009','DD-MON-YYYY'));
    
    
    insert into emp_month values (101,'01-DEC-2008',1432);
    insert into emp_month values (101,'01-JAN-2009',1412);
    insert into emp_month values (101,'01-FEB-2009',1632);
    insert into emp_month values (101,'01-MAR-2009',1672);
    --
    insert into emp_month values (102,'01-DEC-2008',2678);
    insert into emp_month values (102,'01-JAN-2009',2786);
    insert into emp_month values (102,'01-FEB-2009',2883);
    insert into emp_month values (102,'01-MAR-2009',2653);
    insert into emp_month values (102,'01-APR-2009',2653);
    --
    insert into emp_month values (103,'01-NOV-2008',2343);
    insert into emp_month values (103,'01-DEC-2008',2311);
    insert into emp_month values (103,'01-JAN-2009',3122);
    insert into emp_month values (103,'01-FEB-2009',3412);
    insert into emp_month values (103,'01-MAR-2009',3312);
    insert into emp_month values (103,'01-APR-2009',3315);
    insert into emp_month values (103,'01-MAY-2009',4321);
    I use following the QUERY for results. Is it possible to get these same results using ANALYTICAL functions
    or I would appreciate any solution better than this.
    Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
    from emp_month em
    ,(select emp_id,ename,chk_date
    from emp_info)e
    where e.emp_id = em.emp_id
    and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
    group by e.emp_id,e.ename,e.chk_date

    user12212962 wrote:

    I use following the QUERY for results. Is it possible to get these same results using ANALYTICAL functions

    or I would appreciate any solution better than this.

    I have no idea why you need analytic function. I know: there is no need to view inline in your query:

    SQL> Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
      2  from emp_month em
      3  ,(select emp_id,ename,chk_date
      4  from emp_info)e
      5  where e.emp_id = em.emp_id
      6  and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
      7  group by e.emp_id,e.ename,e.chk_date
      8
    SQL> /
    
        EMP_ID ENAME      CHK_DATE    YEAR_AMT
    ---------- ---------- --------- ----------
           102 JOHN       01-APR-09      10975
           101 PAUL       01-MAR-09       4716
           103 KRIS       01-MAY-09      17482
    
    SQL> Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
      2  from emp_month em,
      3       emp_info e
      4  where e.emp_id = em.emp_id
      5  and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
      6  group by e.emp_id,e.ename,e.chk_date
      7  /
    
        EMP_ID ENAME      CHK_DATE    YEAR_AMT
    ---------- ---------- --------- ----------
           102 JOHN       01-APR-09      10975
           101 PAUL       01-MAR-09       4716
           103 KRIS       01-MAY-09      17482
    
    SQL> 
    

    SY.

  • confusion with the analytical functions

    I created an example where I am right now with the help of analytical functions. However, I need the query below to return an additional column. I need to return the result from:-' factor_day_sales * max (sdus)'. Any ideas?

    If the first column is located and must have the following results

    777777, 5791, 10, 1.5, 15, 90, 135, 7050

    the 1350 is the result, I don't know how to do. (some how to multiply factored_day_sales max (sdus) 15 470 = 7050
    create table david_sales (
    pro_id number(38),
    salesidx number (38,6),
    tim_id number(38));
    
    truncate table david_sales
    
    create table david_compensations (
    pro_id number(38),
    tim_id number(38),
    factor number(38,6));
    
    
    insert into david_sales values
    (777777, 10.00, 5795);
    insert into david_sales values
    (777777,20.00, 5795);
    insert into david_sales values
    (777777, 30.00, 5794);
    insert into david_sales values
    (777777, 40.00, 5794);
    insert into david_sales values
    (777777, 100.00, 5793);
    insert into david_sales values
    (777777, 10.00, 5793);
    insert into david_sales values
    (777777,80.00, 5791);
    insert into david_sales values
    (777777, 10.00, 5791);
    
    insert into david_compensations values
    (777777, 5795, 1.5);
    insert into david_compensations values
    (777777, 5793, 2.0);
    insert into david_compensations values
    (777777, 5792, 1.0);
    insert into david_compensations values
    (777777, 5791, 1.5);
    
    
    
        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj 
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795
    Thanks for looking

    Is that what you want?

        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj
        , (s.salesidx * NVL(c.factor, 1) * sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id))
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795
    
    SALES_PRO              COMP_PRO               SALES_TIM              COMP_TIM               DAY_SALES              FACTOR                 FACTORED_DAY_SALES     SDUS                   SUMMJCJ                SUMMEDMULTI
    ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
    777777                 777777                 5791                   5791                   80                     1.5                    120                    90                     135                    10800
    777777                 777777                 5791                   5791                   10                     1.5                    15                     90                     135                    1350  
    

    I get the 1350

    or did you mean:

        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj
        ,  s.salesidx * NVL(c.factor, 1) * (sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id)) summedMulti
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795 
    
    SALES_PRO              COMP_PRO               SALES_TIM              COMP_TIM               DAY_SALES              FACTOR                 FACTORED_DAY_SALES     SDUS                   SUMMJCJ                SUMMEDMULTI
    777777                 777777                 5795                   5795                   10                     1.5                    15                     300                    470                    7050
    

    Note, in the second block, I changed it just to use sumMjCj instead of sDus which seems to correlate with what you wanted (15 * 470 = 7050) while sdus is 15 * 300 = 4500

    Published by: tanging on December 11, 2009 06:17

  • Using the analytical function.

    Hello

    I have this scenario.
    with t as 
    (
    select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id
    from dual)
    select  t.* from t;
    We have two locations for a given item_id. Primary and in bulk.

    I'm trying to get a select statement out of this point of view, where I will be restock the primary AMOUNT of sites in bulk, BUT the smaller bulk first. Once she gets up, I shouldn't take more product.

    There is an analytic function that would do this?

    That's the max I could come up with.
    with t as 
    (
    select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id
    from dual)
    select  t.*, max_qty -
                   (primary_available + SUM(bulk_available)
                    over(PARTITION BY item_id ORDER BY bulk_available)) replen_this_much 
                    from t;
    So, in this scenario, I want to replen bulk_locator_id 100 ' 12122 'and ' 15614 bulk_locator_id 341'. That's all. ZERO of the other rentals (bulk_locator_id). If the question is not clear, please let me know.

    Published by: RPuttagunta on September 11, 2009 16:23

    Hello

    Thanks for posting the sample data.
    It would be useful that you also posted the output you want. Is this?

    .                                 BULK_         REPLEN_
    ITEM_ PRIMARY_   MAX_  BULK_      LOCATOR_        THIS_
    ID    AVAILABLE  QTY   AVAILABLE  ID               MUCH
    ----- ---------- ----- ---------- ---------- ----------
    21009 9          450   100        12122             100
    21009 9          450   524        15614             341
    21009 9          450   2775       8704                0
    21009 9          450   3300       15654               0
    

    If so, you can get to this:

    SELECT       t.*
    ,       GREATEST ( 0
                 , LEAST ( TO_NUMBER (bulk_available)
                         , TO_NUMBER (max_qty)
                        - ( TO_NUMBER (primary_available)
                          + NVL ( SUM (TO_NUMBER (bulk_available))
                                  OVER ( PARTITION BY  item_id
                                         ORDER BY      TO_NUMBER (bulk_available)
                                   ROWS BETWEEN  UNBOUNDED PRECEDING
                                     AND      1           PRECEDING
                                 )
                             , 0
                             )
                          )
                      )
                 ) AS replen_this_much
    FROM       t
    ORDER BY  item_id
    ,            TO_NUMBER (bulk_available)
    ;
    

    You should really store your numbers in NUMBER of columns.

    You essentially posted all what you need analytical functions. The problem was just wrapping this analytical function (or something very close to it) and LESS and more GRAND, so that the replen_this_much column is always between 0 and TO_NUMBER (bulk_available).

Maybe you are looking for