Analytic Functions in PL/SQL

This procedure won't compile - the word PARTITION seems to be the problem - with this error...
PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
Thanks.
CREATE OR REPLACE
PROCEDURE TestAnalyticFunction IS
CURSOR GetAllTransTypes_Cursor IS
select transaction_class.trans_desc,
transaction_code.trans_type ,
transaction_code.trans_code,
transaction_code.trans_code_desc,
sum(tr_tx_amt) as trans_sum,
RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
(PARTITION BY transaction_code.trans_type) AS Percentage
from transaction_code,
transaction_class,
transactions
where TR_POST_DT IS NOT NULL
AND TR_POST_DT >= '01-DEC-2000'
AND TR_POST_DT <= '31-JAN-2001'
AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
group by transaction_code.trans_type,
trans_class,
trans_desc,
trans_code,
trans_code_desc
order by transaction_code.trans_type, trans_code;
TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
TYPE TotalType IS TABLE OF NUMBER(14,2);
TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
TransClassDesc TransClassDescType;
TransCodeType TransCodeTypeType;
TransCodeCode TransCodeCodeType;
TransCodeDesc TransCodeDescType;
Total TotalType;
TotalPct TotalPctType;
BEGIN
OPEN GetAllTransTypes_Cursor;
FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
Total, TotalPct;
CLOSE GetAllTransTypes_Cursor;
END TestAnalyticFunction;
null

Some functions just don't seem to work in PL/SQL even though they work fine in SQL*Plus.
Two such functions I found were NVL2 and RATIO_TO_REPORT.
Have no clue why yet.
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Dale Johnson ([email protected]):
This procedure won't compile - the word PARTITION seems to be the problem - with this error...
PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
Thanks.
CREATE OR REPLACE
PROCEDURE TestAnalyticFunction IS
CURSOR GetAllTransTypes_Cursor IS
select transaction_class.trans_desc,
transaction_code.trans_type ,
transaction_code.trans_code,
transaction_code.trans_code_desc,
sum(tr_tx_amt) as trans_sum,
RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
(PARTITION BY transaction_code.trans_type) AS Percentage
from transaction_code,
transaction_class,
transactions
where TR_POST_DT IS NOT NULL
AND TR_POST_DT >= '01-DEC-2000'
AND TR_POST_DT <= '31-JAN-2001'
AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
group by transaction_code.trans_type,
trans_class,
trans_desc,
trans_code,
trans_code_desc
order by transaction_code.trans_type, trans_code;
TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
TYPE TotalType IS TABLE OF NUMBER(14,2);
TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
TransClassDesc TransClassDescType;
TransCodeType TransCodeTypeType;
TransCodeCode TransCodeCodeType;
TransCodeDesc TransCodeDescType;
Total TotalType;
TotalPct TotalPctType;
BEGIN
OPEN GetAllTransTypes_Cursor;
FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
Total, TotalPct;
CLOSE GetAllTransTypes_Cursor;
END TestAnalyticFunction;<HR></BLOCKQUOTE>
null

Similar Messages

  • 2.1 EA Bug: group by auto complete generates group by for analytic function

    Hi,
    when using an analytic function in the sql text, sqldeveloper generates an automatic group by statement in the sql text.
    Regards,
    Ingo

    Personally, I don't want anything changed automatically EVER. The day you don't notice and you run a wrong statement, the consequences may be very costly (read: disaster).
    Can this be turned off all together? If there's a preference I didn't find, can this be left off by default?
    Thanks,
    K.

  • Using analytical functions in procedure

    I try to use the Corr Function with an Insert Into Command.
    insert into corr_result (CorrVal) select corr(Val1,Val2) from risk_Corr;
    when I use it in SQLPlus directly it works.
    When I use it in a procedure I got the error message :
    PLS-00201: identifier 'CORR' must be declared
    Thanks

    In order to user Analytical Functions in Pl/Sql you must use them with Dynamic Sql
    e.g.,
    EXECUTE IMMEDIATE 'insert into corr_result (CorrVal) select corr(Val1,Val2) from risk_Corr ';
    I try to use the Corr Function with an Insert Into Command.
    insert into corr_result (CorrVal) select corr(Val1,Val2) from risk_Corr;
    when I use it in SQLPlus directly it works.
    When I use it in a procedure I got the error message :
    PLS-00201: identifier 'CORR' must be declared
    Thanks

  • Does sql analytic function help to determine continuity in occurences

    We need to solve this problem in a sql statement.
    imagine a table test with two columns
    create table test (id char(1), begin number, end number);
    and these values
    insert into test('a',1, 2);
    insert into test('a',2,3);
    insert into test('a',3,4);
    insert into test('a',7,10);
    insert into test('a',10,15);
    insert into test('b',5,9);
    insert into test('b',9,21);
    insert into test('c',1,5);
    our goal is to determine continuity in number sequence between begin and end attributes for a same id and determine min and max number from these contuinity chains.
    The result may be
    a, 1, 4
    a, 7, 15
    b, 5, 21
    c, 1, 5
    We test some analytic functions like lag, lead, row_number, min, max, partition by, etc to search a way to identify row set that represent a continuity but we didn't find a way to identify (mark) them so we can use min and max functions to extract extreme values.
    Any idea is really welcome !

    Here is our implementation in a real context for example:
    insert into requesterstage(requesterstage_i, requester_i, t_requesterstage_i, datefrom, dateto )
    With ListToAdd as
    (Select distinct support.requester_i,
    support.datefrom,
    support.dateto
    from support
    where support.datefrom < to_date('01.01.2006', 'dd.mm.yyyy')
    and support.t_relief_i = t_relief_ipar.fgetflextypologyclassitem_i(t_relief_ipar.fismedicalexpenses)
    and not exists
    (select null
    from requesterstage
    where requesterstage.requester_i = support.requester_i
    and support.datefrom < nvl(requesterstage.dateto, support.datefrom + 1)
    and nvl(support.dateto, requesterstage.datefrom + 1) > requesterstage.datefrom)
    ListToAddAnalyzed_1 as
    (select requester_i,
    datefrom,
    dateto,
    decode(datefrom,lag(dateto) over (partition by requester_i order by datefrom),0,1) data_set_start
    from ListToAdd),
    ListToAddAnalyzed_2 as
    (select requester_i,
    datefrom,
    dateto,
    data_set_start,
    sum(data_set_start) over(order by requester_i, datefrom ) data_set_id
    from ListToAddAnalyzed_1)
    select requesterstage_iseq.nextval,
    requester_i,
    t_requesterstage_ipar.fgetflextypologyclassitem_i(t_requesterstage_ipar.fisbefore2006),
    datefrom,
    decode(sign(nvl(dateto, to_date('01.01.2006', 'dd.mm.yyyy')) -to_date('01.01.2006', 'dd.mm.yyyy')), 0, to_date('01.01.2006', 'dd.mm.yyyy'), -1, dateto, 1, to_date('01.01.2006', 'dd.mm.yyyy'))
    from ( select requester_i
    , min(datefrom) datefrom
    , max(dateto) dateto
    From ListToAddAnalyzed_2
    group by requester_i, data_set_id
    );

  • SQL Query With analytical function

    Hi
    Below is the scenario which i am looking for in sql query using analytical functions
    I/p
    Col1
    50
    0
    -150
    -200
    300
    -100
    -300
    500
    -100
    O/p
    Col1          col2
    50                 0
    0                   0
    -150          -100
    -200              -200
    300               0
    -100              0
    -300              -100
    500               400
    -100              0Any help really appreciated
    Thanks in advance
    Edited by: unique on Aug 10, 2010 4:53 AM
    Edited by: unique on Aug 10, 2010 4:55 AM
    Edited by: unique on Aug 10, 2010 4:55 AM

    Oh,In this case,There is OLAP solution ;-)
    OLAP samples of my homepage http://www.geocities.jp/oraclesqlpuzzle/oracle-sql1-olap.html
    with work(SK,Val) as(
    select  1,  50 from dual union
    select  2,   0 from dual union
    select  3,-150 from dual union
    select  4,-200 from dual union
    select  5, 300 from dual union
    select  6,-100 from dual union
    select  7,-300 from dual union
    select  8, 500 from dual union
    select  9,-100 from dual)
    select SK,Val,GID,
    case when Val > 0
         then greatest(0,sum(Val) over(partition by GID))
         else Least(0,Val+greatest(0,sum(Val) over(partition by GID
                                     order by SK rows between unbounded preceding
                                                          and 1 preceding)))
         end as COL3
    from (select SK,Val,
          sum(greatest(0,sign(Val))) over(order by SK) as GID
          from work)
    order by SK;
    SK   VAL  GID  COL3
    1    50    1     0
    2     0    1     0
    3  -150    1  -100
    4  -200    1  -200
    5   300    2     0
    6  -100    2     0
    7  -300    2  -100
    8   500    3   400
    9  -100    3     0

  • SQL Analytical Functions in 9i

    Hi
    I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
    They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
    Cheers

    Hi
    I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
    They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
    Cheers

  • Tuning sql with analytic function

    Dear friends I've developed one sql :
    with REP as
    (select /*+ MATERIALIZE */ branch_code,
       row_number() over(partition by branch_code, account order by bkg_date desc  ) R,
             account,
             bkg_date,
             lcy_closing_bal
        from history t
    select REP1.branch_code,
           REP1.account,
           REP1.bkg_date,
           REP1.lcy_closing_bal,
             NULL  AS second,
           REP2.bkg_date        bkg_date2,
           REP2.lcy_closing_bal lcy_closing_bal2,
            NULL  AS third,
           REP3.bkg_date        bkg_date3,
           REP3.lcy_closing_bal lcy_closing_bal3
      from (SELECT * FROM REP WHERE R=1) REP1, (SELECT * FROM REP WHERE R=2) REP2, (SELECT * FROM REP WHERE R=3) REP3
    where
           (REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
           (REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))The point is I want to restrict (tune) REP before it used ,because , as you can see I need maximum three value from REP (where R=1,R=2,R=3) . Which analytic function and with wich options I have to use to receive only 3 values in each branch_code,account groups at the materializing time ?

    Radrigez wrote:
    Dear friends I've developed one sql :
    with REP as
    from (SELECT * FROM REP WHERE R=1) REP1,
    (SELECT * FROM REP WHERE R=2) REP2,
    (SELECT * FROM REP WHERE R=3) REP3
    where
    (REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
    (REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))
    The first step is to put your subquery (which doesn't need to be materialized) into an inline view and restrict the result set on r in (1,2,3) as suggested by thtsang - you don't need to query the same result set three times.
    Then you're looking at a simple pivot operation (assuming the number of rows you want per branch and account is fixed). If you're on 11g search the manuals for PIVOT, on earlier versions you can do this with a decode() or case() operator.
    Step 1 (which could go into another factored subquery) would be something like:
    select
            branch_code, account,
            case r = 1 then bkg_date end bkg_date,
            case r = 1 then lcy_closing_bal end lcy_closing_bal,
            case r = 2 then bkg_date end bkg_date2,
            case r = 2 then lcy_closing_bal end lcy_closing_bal2,
            case r = 3 then bkg_date end bkg_date3,
            case r = 3 then lcy_closing_bal end lcy_closing_bal3
    from
            repThis gives you the eight necessary columns, but still (up to) three rows per branch/account.
    Then you aggregate this (call it rep1) on branch and account.
    select
            branch_code, account,
            max(bkg_date),
            max(lcy_closing_bal),
            max(bkg_date2),
            max(lcy_closing_bal2),
            max(bkg_date3),
            max(lcy_closing_bal3)
    from
            rep1
    group by
            branch_code, account
    order by
            branch_code, accountRegards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Author: <b><em>Oracle Core</em></b>

  • Surprised that 1Z0-047 (SQL Expert) does not include Analytic Functions

    Apparently, from a reading of the 1Z0-047 outline,  Analytic Functions are not "in scope" for the Exam.
    Comments, anyone ?
    Hemant K Chitale

    Comments, anyone ?
    There are definitely some gaps in the SQL coverage on that exam. I could see the exam being redone (and improved) for 12C much as 1Z0-144 was rewritten for 11G as a much improved version of 1Z0-147.  I'd like to see them remove the individual listing of topics pulled from SQL Fundamentals.  They should be replaced with either a much-compressed list or a single note that knowledge of SQL fundamentals is assumed.  This might help with the level of confusion newbies have over which test they should take.
    Adding all of the functions you mention would not only make the test more comprehensive but also allow the tone of the exam to be altered.  Right now I consider it to be filled with questions intended to misdirect the test candidate.  The questions are 'tricky' rather than 'difficult'. This is largely because SQL is inherently easy to read so the only way to make it hard *is* to write deliberately bad SQL. Adding more advanced topics would allow for more questions that don't have to be made artificially more difficult.

  • Analytic function sql help

    Table with 2 columns pro_id,sub_ver_id. Need only 5 pro_id for each sub_ver_id
    SQL> select * from test1 order by SUB_VER_ID;
    PRO_ID SUB_VER_ID
    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    10 1
    15 1
    16 1
    11 1
    12 1
    PRO_ID SUB_VER_ID
    13 1
    14 1
    11 2
    12 3
    I'm new to analytic function i got query as below, but not able to get an idea to limit the SRLNO to only 5 rows for each SUB_VER_ID. Any hint would be much appreciated.
    select distinct sub_ver_id,pro_id, row_number () over (order by sub_ver_id) srlno
    from test1 order by sub_ver_id

    May be as below..
    select *
    from
    select sub_ver_id,pro_id, row_number () over (partition by sub_ver_id order by null) srlno
    from test1
    ) where srlno <=5 order by sub_ver_idThanks...

  • SQL Analytic Functions - Licencing

    Hi,
    I am not sure, so I ask. Are SQL Analytical Functions (LEAD, LAG, construction OVER (PARTITION BY ORDER BY)) available in all versions of Oracle 11g? Especially Oracle 11g Standard Edition?
    dino

    Analytic Functions were an EE feature in 8i and moved into the SE edition when 9i was released. To my knowledge Oracle hasn't changed anything in its licensing scheme regarding this feature. As a sidenote: analytic functions are also available in the XE edition.
    C.

  • SQL using analytical function

    Hi all,
    I want an help in creating my SQL query to extract the data described below:
    I have one table example test containing data like below:
    ID     Desc     Status
    1     T1          DEACTIVE
    2     T2          ACTIVE
    3     T3          SUCCESS
    4     T4          DEACTIVE
    The thing i want to do is selecting all lines with ACTIVE status in this table but is there is no ACTIVE status, my query will give me the last line with DEACTIVE status.
    Can I do this in one query by using analytical function for example, if yes can yiu help me on thaht query.
    regards,
    Raluce

    Hi, Raluce,
    Here's one way to do that:
    WITH got_r_num AS
        SELECT  deptno, ename, job, hiredate
        ,       ROW_NUMBER () OVER ( PARTITION BY  deptno
                                     ORDER BY      job
                                     ,             hiredate  DESC
                                   )  AS r_num
        FROM    scott.emp
        WHERE   job  IN ('ANALYST', 'CLERK')
    SELECT     deptno, ename, job, hiredate
    FROM       got_r_num
    WHERE      job     = 'ANALYST'
    OR         r_num   = 1
    ORDER BY   deptno
    Since I don't have a sample version of your table, I used scott.emp to illustrate.
    Output:
        DEPTNO ENAME      JOB       HIREDATE
            10 MILLER     CLERK     23-JAN-82
            20 SCOTT      ANALYST   19-APR-87
            20 FORD       ANALYST   03-DEC-81
            30 JAMES      CLERK     03-DEC-81
    This query finds all ANALYSTs in each department, regardless of how many there are.  (Deptno 20 happens to have 2 ANALYSTs.)  If there is no ANALYST in a department, then the most recently hired CLERK is included.  (Deptnos 10 and 30 don't have any ANALYSTs.)
    This "partitions", or sub-divides, the table into separate units, one for each department.  In the problem you posted, it looks like you want to operate in the entire table, without sub-dividing it in any way.  To do that, just omit the PARTITION BY clause in the analytic ROW_NUMBER function, like this:
    WITH got_r_num AS
        SELECT  deptno, ename, job, hiredate
        ,       ROW_NUMBER () OVER ( --  PARTITION BY  deptno
                                     ORDER BY      job
                                     ,             hiredate  DESC
                                   )  AS r_num
        FROM    scott.emp
        WHERE   job  IN ('ANALYST', 'CLERK')
    SELECT     deptno, ename, job, hiredate
    FROM       got_r_num
    WHERE      job     = 'ANALYST'
    OR         r_num   = 1
    ORDER BY   deptno

  • How to get LAST_VALUE from an Analytic Function within a report?

    My analytical report has following sql:
    SELECT....
    DENSE_RANK() OVER (PARTITION BY pco.appropriation ORDER BY pco.appropriation,
    pco.fiscal_year ASC NULLS LAST) "Duration Years",
    SUM(pco.quantity) OVER (PARTITION BY sms.data_source ORDER BY
    sms.data_source, cst.display_nm RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
    ROW) "Quantity Cum",
    FROM ........
    I have a request to calculate the percentage value by dividing MAX or LASTVALUE of these two functions at current row level:_
    %DENSE_RANK = LAST_VALUE of ? DENSE_RANK() OVER (PARTITION BY pco.appropriation ORDER BY pco.appropriation,
    pco.fiscal_year ASC NULLS LAST) "Duration Years"
    / (divide by)
    DENSE_RANK() OVER (PARTITION BY pco.appropriation ORDER BY pco.appropriation,
    pco.fiscal_year ASC NULLS LAST) "Duration Years"
    %SUM = LAST_VALUE of ? SUM(pco.quantity) OVER (PARTITION BY sms.data_source ORDER BY
    sms.data_source, cst.display_nm RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
    ROW) "Quantity Cum",
    / (divide by)
    SUM(pco.quantity) OVER (PARTITION BY sms.data_source ORDER BY
    sms.data_source, cst.display_nm RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
    ROW) "Quantity Cum"
    Example of report output:
    DENSE RANK DENSE RANK % SUM SUM %
    1 10% 10 10%
    2 20% 20 20%
    3 100% 30 100%
    1 10% 10 10%
    2 20% 20 20%
    I appreciate your help to implement LAST_VALUE function for this case. Many thanks in advance.
    Rob.
    Edited by: user10455955 on Dec 17, 2008 9:25 AM

    Hi Rod,
    Thank you for replying. My intentions are:
    1. to get last value of DENSE_RANK(), and divide by fisrt until last DENSE_RANK() value within defined partition.
    Example: I have DENSE_RANK() from 1 to 7, and want to divide last value (7) by 1, 2,... so on until 7, so last value 7 always renders 100%
    2. to get last value of cumulative total, and divide fisrt until and last cumulative total value within defined partition
    Example: I have 3 values 1, 2, 3. Fisrt cumulative total is 1 , second cumulative is 3 (1+2) , third cumulative is 6 (1+2+3). I want to divide last cumulative total 6 by 1, then 2, then 3.
    Thanks again.
    Rob.
    Edited by: user10455955 on Dec 22, 2008 9:16 AM
    Edited by: user10455955 on Dec 22, 2008 9:18 AM

  • Aggregation of analytic functions not allowed

    Hi all, I have a calculated field called Calculation1 with the following calculation:
    AVG(Resolution_time) KEEP(DENSE_RANK FIRST ORDER BY RANK ) OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report #7 COMPL".Resource Name )
    The result of this calculation is correct, but is repeated for all the rows I have in the dataset.
    Group Name      Resourse name    Calculation1
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    5112 rowsI tried to create another calculation in order to have only ONE value for the couple "Group Name, Resource Name) as AVG(Calculation1) but I have the error: Aggregation of analytic functions not allowed
    I saw also inside the "Edit worksheet" panel that the Calculation1 *is not represented* with the "Sigma" symbol I(as for example a simple AVG(field_1)) and inside the SQL code I don't have GROUP BY Group Name, Resource Name......
    I'd like to see ONLY one row as:
    Group Name      Resourse name    Calculation1
    SH Group            Mr. A            10....that it means I grouped by Group Name, Resource Name
    Anyone knows how can I achieve this result or any workarounds ??
    Thanks in advance
    Alex

    Hi Rod unfortunately I can't use the AVG(Resolution_time) because my dataset is quite strange...I explain to you better.
    Ι start from this situation:
    !http://www.freeimagehosting.net/uploads/6c7bba26bd.jpg!
    There are 3 calculated fields:
    RANK is the first calculated field:
    ROW_NUMBER() OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report Assigned To & Created By COMPL".Resource Name,"Tickets Report Assigned To & Created By COMPL".Incident Id  ORDER BY  "Tickets Report Assigned To & Created By COMPL".Select Flag )
    RT Calc is the 2nd calculation:
    CASE WHEN RANK = 1 THEN Resolution_time END
    and Calculation2 is the 3rd calculation:
    AVG(Resolution_time) KEEP(DENSE_RANK FIRST ORDER BY  RANK ) OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report Assigned To & Created By COMPL".Resource Name )
    As you can see, from the initial dataset, I have duplicated incident id and a simple AVG(Resolution Time) counts also all the duplication.
    I used the rank (based on the field "flag) to take, for each ticket, ONLY a "resolution time" value (in my case I need the resolution time when the rank =1)
    So, with the Calculation2 I calculated for each couple Group Name, Resource Name the right AVG(Resolution time), but how yuo can see....this result is duplicated for each incident_id....
    What I need instead is to see *once* for each couple 'Group Name, Resource Name' the AVG(Resolution time).
    In other words I need to calculate the AVG(Resolution time) considering only the values written inside the RT Calc fields (where they are NOT NULL, and so, the total of the tickets it's not 14, but 9).
    I tried to aggregate again using AVG(Calculation2)...but I had the error "Aggregation of analytic functions not allowed"...
    Do you know a way to fix this problem ?
    Thanks
    Alex

  • Case Statement in Analytic Function SUM(n) OVER(PARTITION BY x)

    Hi Guys,
    I have the following SQL that doesn't seem to consider the When clause I am using in the case staement inside the analytic function(SUM). Could somebody let me know why? and suggest the solution?
    Select SUM(Case When (A.Flag = 'B' and B.Status != 'C') Then (NVL(A.Amount_Cr, 0) - (NVL(A.Amount_Dr,0))) Else 0 End) OVER (PARTITION BY A.Period_Year) Annual_amount
         , A.period_year
         , B.status
    , A.Flag
    from A, B, C
    where A.period_year = 2006
    and C.Account = '301010'
    --and B.STATUS != 'C'
    --and A.Flag = 'B'
    and A.Col_x = B.Col_x
    and A.Col_y = C.Col_y
    When I use this SQL, I get
    Annual_Amount Period_Year Status Flag
    5721017.5 --------- 2006 ---------- C -------- B
    5721017.5 --------- 2006 ---------- O -------- B
    5721017.5 --------- 2006 ---------- NULL ----- A
    And when I put the conditions in the where clause, I get
    Annual_Amount Period_Year Status Flag
    5721017.5 ---------- 2006 ---------- O -------- B

    Here are some scripts,
    create table testtable1 ( ColxID number(10), ColyID number(10) , Periodname varchar2(15), Flag varchar2(1), Periodyear number(15), debit number, credit number)
    insert into testtable1 values(1, 1000, 'JAN-06', 'A', 2006, 7555523.71, 7647668)
    insert into testtable1 values(2, 1001, 'FEB-06', 'B', 2006, 112710, 156047)
    insert into testtable1 values(3, 1002, 'MAR-06', 'A', 2006, 200.57, 22376.43)
    insert into testtable1 values(4, 1003, 'APR-06', 'B', 2006, 0, 53846)
    insert into testtable1 values(5, 1004, 'MAY-06', 'A', 2006, 6349227.19, 6650278.03)
    create table testtable2 ( ColxID number(10), Account number(10))
    insert into testtable2 values(1, 300100)
    insert into testtable2 values(2, 300200)
    insert into testtable2 values(3, 300300)
    insert into testtable2 values(4, 300400)
    insert into testtable2 values(5, 300500)
    create table apps.testtable3 ( ColyID number(10), Status varchar2(1))
    insert into testtable3 values(1000, 'C')
    insert into testtable3 values(1001, 'O')
    insert into testtable3 values(1002, 'C')
    My SQL:
    select t1.periodyear
         , SUM(Case When (t1.Flag = 'B' and t3.Status != 'C') Then (NVL(t1.credit, 0) - (NVL(t1.debit,0))) Else 0 End) OVER (PARTITION BY t1.PeriodYear)
         Annual_amount
         , t1.flag
         , t3.status
         , t2.account
    from testtable1 t1, testtable2 t2, testtable3 t3
    where t1.colxid = t2.colxid
    and t1.colyid = t3.colyid(+)
    --and t1.Flag = 'B' and t3.Status != 'C'
    Result:
    PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
    2006 ------------------ 43337 --------------- A ----------- C ---------- 300100
    2006 ------------------ 43337 --------------- B ----------- O ---------- 300200
    2006 ------------------ 43337 --------------- A ----------- C ---------- 300300
    2006 ------------------ 43337 --------------- B ------------ ----------- 300400
    2006 ------------------ 43337 --------------- A ------------ ----------- 300500
    With condition "t1.Flag = 'B' and t3.Status != 'C'" in where clause instead of in Case statement, Result is (which is desired)
    PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
    2006 ------------------ 43337 --------------- B ----------- O ---------- 300200

  • Analytic function to retrieve a value one year ago

    Hello,
    I'm trying to find an analytic function to get a value on another row by looking on a date with Oracle 11gR2.
    I have a table with a date_id (truncated date), a flag and a measure. For each date, I have at least one row (sometimes 2), so it is gapless.
    I would like to find analytic functions to show for each date :
    sum of the measure for that date
    sum of the measure one week ago
    sum of the measure one year ago
    As it is gapless I managed to do it the week doing a group by date in a subquery and using a LAG with offset set to 7 on top of it (see below).
    However I'm struggling on how to do that for the data one year ago as we might have leap years. I cannot simply set the offset to 365.
    Is it possible to do it with a RANGE BETWEEN window clause? I can't manage to have it working with dates.
    Week :LAG with offset 7
    SQL Fiddle
    or
    create table daily_counts
      date_id date,
      internal_flag number,
      measure1 number
    insert into daily_counts values ('01-Jan-2013', 0, 8014);
    insert into daily_counts values ('01-Jan-2013', 1, 2);
    insert into daily_counts values ('02-Jan-2013', 0, 1300);
    insert into daily_counts values ('02-Jan-2013', 1, 37);
    insert into daily_counts values ('03-Jan-2013', 0, 19);
    insert into daily_counts values ('03-Jan-2013', 1, 14);
    insert into daily_counts values ('04-Jan-2013', 0, 3);
    insert into daily_counts values ('05-Jan-2013', 0, 0);
    insert into daily_counts values ('05-Jan-2013', 1, 1);
    insert into daily_counts values ('06-Jan-2013', 0, 0);
    insert into daily_counts values ('07-Jan-2013', 1, 3);
    insert into daily_counts values ('08-Jan-2013', 0, 33);
    insert into daily_counts values ('08-Jan-2013', 1, 9);
    commit;
    select
        date_id,
        total1,
        LAG(total1, 7) OVER(ORDER BY date_id) total_one_week_ago
      from
          select
            date_id,
            SUM(measure1) total1
          from daily_counts
          group by date_id
    order by 1;
    Year : no idea?
    I can't give a gapless example, would be too long but if there is a solution with the date directly :
    SQL Fiddle
    or add this to the schema above :
    insert into daily_counts values ('07-Jan-2012', 0, 11);
    insert into daily_counts values ('07-Jan-2012', 1, 1);
    insert into daily_counts values ('08-Jan-2012', 1, 4);
    Thank you for your help.
    Floyd

    Hi,
    Sorry, I;m not sure I understand the problem.
    If you are certain that there is at least 1 row for every day, then you can be sure that the GROUP BY will produce exactly 1 row per day, and you can use LAG (total1, 365) just like you already use LAG (total1, 7).
    Are you concerned about leap years?  That is, when the day is March 1, 2016, do you want the total_one_year_ago column to reflect March 1, 2015, which was 366 days earlier?  If that case, use
    date_id - ADD_MONTHS (date_id, -12)
    instead of  365.
    LAG only works with an exact number, but you can use RANGE BETWEEN with other analytic functions, such as MIN or SUM:
    SELECT DISTINCT
              date_id
    ,         SUM (measure1) OVER (PARTITION BY date_id)    AS total1
    ,         SUM (measure1) OVER ( ORDER BY      date_id
                                    RANGE BETWEEN 7 PRECEDING
                                          AND     7 PRECEDING
                                  )                       AS total1_one_week_ago
    ,         SUM (measure1) OVER ( ORDER BY      date_id
                                    RANGE BETWEEN 365 PRECEDING
                                          AND     365 PRECEDING
                                  )                       AS total1_one_year_ago
    FROM      daily_counts
    ORDER BY  date_id
    Again, use date arithmetic instead of the hard-coded 365, if that's an issue.
    As Hoek said, it really helps to post the exact results you want from the given sample data.  You're miles ahead of the people who don't even post the sample data, though.
    You're right not to post hundreds of INSERT statements to get a year's data.  Here's one way to generate sample data for lots of rows at the same time:
    -- Put a 0 into the table for every day in 2012
    INSERT INTO daily_counts (date_id, measure1)
    SELECT  DATE '2011-12-31' + LEVEL
    ,       0
    FROM    dual
    CONNECT BY LEVEL <= 366

Maybe you are looking for

  • Attribute Change run taking too long time to complete.

    Hi all, Attribute change run has been taking too long time to complete.It has to realign 50 odd aggreagates, some by delta , some by reconstruction. But inspite of all the aggregates it used to finish in quick time earlier. But since last 4-5 days it

  • How can i recover my database after losing system data file.

    hi everyone, how can i recover my database in the following scenario. 1. offline complete backup taken 2 days ago. database was in archive mode. 2. today i lost my system data file, and also lost my all archived files. 3. i started up the database bu

  • Changing content in an array

    can someone help me with changing content in an array. The index will be specified by the user entering the index number. eg Enter the item number to Change 1 Then I need to replace the data in this index with new data. I am very new to java so I am

  • Dreamweaver and Fireworks will not run

    when starting mx 2004 dreamweaver or fireworks, and immediate error message pops up: ERRO: Write File() The handle is invalid. the application failed to initialize properly (0xc0000142) Click ok to terminate application. flash works fine even after r

  • Volume button not working with Vista Business

    Anybody knows what Toshiba Software should i install to be able to use the volume button in Vista Business? I have a Qosmio G20-102. Thank you.