Division with aggregate function

Hi all
I want to calculate the count (emp_id) count (dept_id) division.

under query does not work. could someone please suggest

Select (a / b) * 100 of ((select count (emp_id) in emp) a, b (select count (dept_id) of the Department))

Resp. bad, no aliasing column

select
 a/b*100
from
 (select count(emp_id) a from emp)
,(select count(dept_id) b from dept)

or even

select
 count(*)/
 max((select count(*) from dept))
 *100
from
emp 

You must not fear Division by as long as you're yure, there are all the lines in the Dept.

otherwise use nullif

select
 count(*)/
 nullif(max((select count(*) from dept where 1=2)),0)
 *100
from
emp

Published by: chris227 on 12.12.2012 05:00

Published by: chris227 on 12.12.2012 05:01

Published by: chris227 on 12.12.2012 05:04
confused

Tags: Database

Similar Questions

  • Can be used with aggregate functions on large total rollup of the column

    Hi all

    I'm trying manipulate/aggregate on the grand total of the columns in the rollup package, can I do this? I use ROLL upward to avoid many groups nested clauses as below.

    GROUP OF base.campaign, cij.avordertype, cij.invoiceaccount

    )

    )

    Campaign of GROUP BY, invoiceaccount

    ) campaign group;

    with

    base_t as

    (CHECK THE ICJ. AVBILLINGCAMPAIGN CAMPAIGN, ICJ. ORDERTYPE AVORDERTYPE,

    NVL (SUM (CASE WHEN ICJ. (AVORDERTYPE = '01' THEN AVAMOUNT END CH.), 0) SUMAVAMOUNT_01,

    SUM (CH. SUMAVAMOUNT_ALL AVAMOUNT),

    CIJ.avordertype,

    CIJ.invoiceaccount,

    MIN (CIJ.invoiceid) firstinvoice_inrange,

    COUNT (SEPARATE ICJ. REFFACTURE bills),

    Sum (ch.avamount) invoicesamount

    NVL (SUM (CASE

    WHEN cij.avordertype = '01' CAN

    COUNT (SEPARATE ICJ. REFFACTURE)

    (END), 0)

    OF CUSTINVOICEJOUR ICJ JOIN AVCOURIERHIST CH

    ON substr (nls_lower (ICJ. DATAAREAID), 1, 7) = substr (nls_lower (CH. DATAAREAID), 1, 7)

    AND SUBSTR (NLS_LOWER (ICJ. REFFACTURE), 1, 43) = SUBSTR (NLS_LOWER (CH. AVINVNUMBER), 1, 43)

    WHERE substr (nls_lower (CH. DATAAREAID), 1, 7) = "201" AND SUBSTR (NLS_LOWER(CIJ.)) AVORDERTYPE), 1: 21) IN ('01 ', ' 09')

    AND SUBSTR (NLS_LOWER (CH. AVCARGOCOMPANYID), 1: 25) = '06333675'

    AND CH. AVINITIALDATE BETWEEN to_date ("'31.03.2014', ' dd.mm.yyyy") AND to_date ("'31.03.2014', ' dd.mm.yyyy")

    GROUP BY ROLLUP (ICJ. AVBILLINGCAMPAIGN, ICJ. AVORDERTYPE, ICJ. INVOICEACCOUNT))

    Select * from base_t;

    Moreover, I can do anything with the help of ROLL UP clause instead of the bottom?

    GROUP OF base.campaign, cij.avordertype, cij.invoiceaccount

    )

    )

    Campaign of GROUP BY, invoiceaccount

    ) campaign group;

    Concerning

    Charlie

    ROLLUP is used to obtain the aggregate more or less value. Here is an example

    For the subtotal of wages to each level of deptno.

    SQL > select deptno, empno, sum (sal)
    2 of PEM
    Group 3
    4 by deptno, rollup (empno);

    DEPTNO EMPNO SUM (SAL)
    ---------- ---------- ----------
    10 7782 12450
    10-7839-5000
    10 17450
    20 7369 12975
    20 7566 12975
    20 7788 13000
    20 7876 11101


    20 50051
    30 7499 11600
    30 7521 11250
    30 7654 11250
    30 7698 12850
    30 7844 11500
    30 58450

    14 selected lines.

    To get the subtotal of wages to each level of deptno and finally get a grand total

    SQL > select deptno, empno, sum (sal)
    2 of PEM
    Group 3
    4 by cumulative (deptno, empno);

    DEPTNO EMPNO SUM (SAL)
    ---------- ---------- ----------
    10 7782 12450
    10-7839-5000
    10 17450
    20 7369 12975
    20 7566 12975
    20 7788 13000
    20 7876 11101
    20 50051
    30 7499 11600
    30 7521 11250
    30 7654 11250
    30 7698 12850
    30 7844 11500
    30 58450
    125951

    15 selected lines.

  • ROWNUM works only with aggregate functions

    Hi all

    I need 5 sales channel of the high sage and wise year, I wrote the following query:

    SELECT CH. CHANNEL_DESC, TM. CALENDAR_YEAR, SUM (SA. AMOUNT_SOLD) AS SALES_AMT OF CHANNELS CH, TM TIME, ITS SALE
    WHERE CH. CHANNEL_ID = SA. CHANNEL_ID AND TM. TIME_ID = SA. TIME_ID AND ROWNUM < = 5
    CH. CHANNEL_DESC, TM GROUP. CALENDAR_YEAR ORDER BY SALES_AMT DESC

    but it is only 1 row instead of 5

    There is no problem with the data, I asked without ROWNUM more lines are to come, I can use the rank function, but you want to know the reason why, one line is returned.

    Thank you
    Anthony.

    You are only 5 rows back and then grouping them.

    select * from
        (
        select ch.channel_desc, tm.calendar_year, sum(sa.amount_sold) as sales_amt
        from channels ch, times tm, sales sa
        where ch.channel_id = sa.channel_id
        and tm.time_id = sa.time_id
        group by ch.channel_desc, tm.calendar_year
        order by sales_amt desc
        )
    where rownum <= 5
    
  • Data model with decimal point in aggregate functions

    Hello

    In the data model when a summary aggregate function type is created, the decimal fields are displayed with ',' instead of '. '.

    Is it possible to tell displayed with decimal BEEP '. '?

    I solve it by copying the CS attribute and create a new by an element of expresion.

  • Why don't PIVOT clause works with aggregate TRIP of 11g function?

    Hi all!

    I'm really confused as to what is considered an aggregate function in the context of the PIVOT clause in 11 g.

    I've been playing with a lot of things related to collections lately and it took birth in an aside:
    CREATE TABLE TEST_COLL
    (
       NODE_ID    VARCHAR2(15 CHAR) NOT NULL,
       NODE_VALUE VARCHAR2(45 CHAR) NOT NULL,
       NODE_LEVEL NUMBER(1)         NOT NULL 
    );
    
    CREATE OR REPLACE TYPE TREE_NODE AS OBJECT 
    (
       NODE_KEY  VARCHAR2( 15 CHAR),
       NODE_NAME VARCHAR2(127 CHAR)
    );
    
    CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL;
    At this point, I'm sure we can all agree that the application
    SELECT NODE_LEVEL, 
           CAST(COLLECT(TREE_NODE(NODE_ID, NODE_VALUE)) AS TREE_NODES) AS NODES
      FROM TEST_COLL
     GROUP BY NODE_LEVEL;
    perfectly valid is that the function of COLLECTION is an aggregation function according to the [Official Documentation | http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions031.htm#i1271564]
    But then, one of the two following queries should work
    SELECT CAST(REGION_NODES     AS TREE_NODES) AS REGIONS,
           CAST(DEPARTMENT_NODES AS TREE_NODES) AS DEPARTMENTS,
           CAST(AREA_NODES       AS TREE_NODES) AS AREAS,
           CAST(CENTRE_NODES     AS TREE_NODES) AS CENTRES
      FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM TREE_COLL
           )
     PIVOT (COLLECT(NODE) FOR NODE_LEVEL IN (1 AS REGION_NODES,
                                             2 AS DEPARTMENT_NODES,
                                             3 AS AREA_NODES,
                                             4 AS CENTRE_NODES
                                            )
           );
    
    or (better)
    
    SELECT REGION_NODES     AS REGIONS,
           DEPARTMENT_NODES AS DEPARTMENTS,
           AREA_NODES       AS AREAS,
           CENTRE_NODES     AS CENTRES
      FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM TREE_COLL
           )
     PIVOT (CAST(COLLECT(NODE) AS TREE_NODES) FOR NODE_LEVEL IN (1 AS REGION_NODES,
                                                                 2 AS DEPARTMENT_NODES,
                                                                 3 AS AREA_NODES,
                                                                 4 AS CENTRE_NODES
                                                                )
           );
    Yet, these two with
    ORA-56902: expect aggregate function inside pivot operation
    Study further, I found the same behavior when you use the aggregate function in the PIVOT XMLAGG clause.

    Is this normal? And if this is the case, is there another way to get the result that I expected?

    My version is
    SQL> SELECT BANNER FROM V$VERSION;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE    11.2.0.3.0      Production
    TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    Thanks in advance
    Best regards

    Philippe

    Most likely a bug. But you can work around it by using any other aggregate for this group is composed of a single line and apply collect a rotated value. Yes, the cost is double aggregation. And also there is another cost - you must create the service card member other aggreations such as MAX/MIN, etc. will not work:

    CREATE OR REPLACE TYPE TREE_NODE AS OBJECT
    (
       NODE_KEY  VARCHAR2( 15 CHAR),
       NODE_NAME VARCHAR2(127 CHAR),
       map member function f return varchar2
    )
    /
    
    Type created.
    
    CREATE OR REPLACE TYPE BODY TREE_NODE AS
      map member function f return varchar2 is
      begin
         return NODE_NAME;
      end f;
    end;
    /
    
    Type body created.
    
    CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL
    /
    
    Type created.
    
    SQL> select  *
      2    from  test_coll
      3  /
    
    NODE_ID NODE_VALUE NODE_LEVEL
    ------- ---------- ----------
    1       A                   1
    2       B                   2
    3       C                   3
    4       D                   4
    5       E                   1
    6       F                   2
    7       G                   3
    8       H                   4
    
    8 rows selected.
    
    SQL> 
    

    Now:

    SELECT CAST(COLLECT(REGION_NODES)     AS TREE_NODES) AS REGIONS,
           CAST(COLLECT(DEPARTMENT_NODES) AS TREE_NODES) AS DEPARTMENTS,
           CAST(COLLECT(AREA_NODES)       AS TREE_NODES) AS AREAS,
           CAST(COLLECT(CENTRE_NODES)     AS TREE_NODES) AS CENTRES
      FROM (
            SELECT  ROWID RID,
                    NODE_LEVEL,
                    TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM  TEST_COLL
           )
     PIVOT (MAX(NODE) FOR NODE_LEVEL IN (
                                         1 AS REGION_NODES,
                                         2 AS DEPARTMENT_NODES,
                                         3 AS AREA_NODES,
                                         4 AS CENTRE_NODES
                                        )
           )
    /
    
    REGIONS(NODE_KEY, NODE_NAME)                         DEPARTMENTS(NODE_KEY, NODE_NAME)                     AREAS(NODE_KEY, NODE_NAME)                           CENTRES(NODE_KEY, NODE_NAME)
    ---------------------------------------------------- ---------------------------------------------------- ---------------------------------------------------- ----------------------------------------------------
    TREE_NODES(TREE_NODE('1', 'A'), TREE_NODE('5', 'E')) TREE_NODES(TREE_NODE('6', 'F'), TREE_NODE('2', 'B')) TREE_NODES(TREE_NODE('7', 'G'), TREE_NODE('3', 'C')) TREE_NODES(TREE_NODE('8', 'H'), TREE_NODE('4', 'D'))
    
    SQL> 
    

    SY.

  • Problem with my aggregate function

    Hello

    I tried to build my own global function in the chain, but I have a problem when I use it in my application. When I'm with her, he wrote ORA904, considering that this idenfifier exists. Do you know what is the problem?

    here my aggregate function:
    create or replace FUNCTION aggme (query_in in VARCHAR2) RETURN VARCHAR2 IS
        incoming    varchar2(4000);
        hold_result varchar2(4000);
        c sys_refcursor;
    Begin
        open c for query_in;
        loop
            fetch c into incoming;
            exit when c%notfound;
            hold_result := hold_result||','||incoming;
        end loop;
        return ltrim(hold_result,',');
    END;
    and here my request using my aggregate function aggme:
    select
      RES.LASTNAME as "LASTNAME",
      RES.FIRSTNAME as "FIRSTNAME",
      aggme('select NAME from PATHOLOGY where ID= RES.PATHOLOGY_ID') as "PATHOLOGY"
    from
      TBK_RESOURCE RES

    user539451 wrote:
    Hi BluShadow,

    I followed your advice and try to use the aggregation of clob. No problem to compile the function but when I use the clobagg to my query, is not aggregated. Perhaps, I don't use it correctly.

    select
    RES.PATIENT_LASTNAME as "LASTNAME",
    RES.PATIENT_FIRSTNAME as "FIRSTNAME",
    (select trim(',' from clobagg(name||',')) as "pathology" from pathology where ID= RES.PATHOLOGY_ID)
    FROM
    TBK_RESOURCE RES
    

    output, I have:

    FIRSTNAME. LASTNAME. PATHOLOGY
    TOTO | OTOT | ACB
    TOTO | OTOT | GB

    instead of having:
    TOTO OTOT ACB, GB

    Well, I don't have data to work with, but it works well for me...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select 'TOTO' as FIRSTNAME, 'OTOT' as LASTNAME, 'PBR' as PATHOLOGY from dual union all
      2             select 'TOTO', 'OTOT', 'GB' from dual)
      3  --
      4  -- end of test data
      5  --
      6  select firstname, lastname
      7        ,trim(',' from clobagg(pathology||',')) as pathology
      8  from t
      9* group by firstname, lastname
    SQL> /
    
    FIRS LAST PATHOLOGY
    ---- ---- --------------------------------------------------------------------------------
    TOTO OTOT PBR,GB
    
  • Bug with the aggregate function and no group

    When I run the following query:
    with the_table as
    (
      select 1 as id, 100 as cost from dual
      union all select 2 as id, 200 as cost from dual
      union all select 3 as id, 300 as cost from dual
      union all select 4 as id, 400 as cost from dual
      union all select 5 as id, 500 as cost from dual
    )
    select id, cost
    from
    (
      select id, cost
      from the_table
      --
      union all
      --
      select 0 as id, sum(cost) as cost
      from the_table
      where 0 = 1
      -- group by 1
    )
    order by id;
    I get this result:
    ID    COST
    --  ------
     0  <null>     
     1     100
     2     200
     3     300
     4     400
     5     500
    However, when I "uncomment" the line "Group 1", the query works as expected (without the id = rank 0).

    This occurs even when "the_table" is an array.

    Someone else comes through this (and if so, how long is a problem)?

    The database is 11.2.0.2.0 64-bit.

    EDIT: It also happens without a Union - the following returns a single line (with null 0 and cost of id) without the Group By and no line with her:
    select id, cost
    from
    (
      select 0 as id, sum(cost) as cost
      from 
      (
        select 1 as id, 100 as cost from dual
        union all select 2 as id, 200 as cost from dual
        union all select 3 as id, 300 as cost from dual
        union all select 4 as id, 400 as cost from dual
        union all select 5 as id, 500 as cost from dual
      )
      where 0 = 1
      -- group by 1
    )
    order by id
    Edited by: Donbot February 15, 2012 10:29

    Donbot wrote:
    Someone else comes through this (and if so, how long is a problem)?

    The database is 11.2.0.2.0 64-bit.

    This is a documented behavior.

    http://docs.Oracle.com/CD/E11882_01/server.112/e26088/functions003.htm#SQLRF20035

    "
    All except COUNT (*) GROUPING and GROUPING_ID aggregate functions ignore NULL values. You can use the NVL function in the argument of an aggregation function to substitute a value for a null value. COUNTY and REGR_COUNT never return null, but return a number or zero. For all remaining functions of aggregation, * if the DataSet contains no line, * or if it contains only the rows with NULL values as arguments to the aggregate function, * then the function returns null.*
    "

  • Aggregate function with joining

    Hi all
    I have the following SQL query, and I want to add a join with it:
    SELECT Collected_Goods.ProductId, SUM(Collected_Goods.Amount)
    FROM Collected_Goods
    GROUP BY Collected_Goods.ProductId
    and I want to replace or add (but it is better to replace) the Collected_Goods. ProductId with a product. Name where the product name is displayed in another table called products. I also want the SUM aggregate function to work on Collected_Goods.ProductId

    FK in Collected_Goods to the products is Collected_Goods.ProductId and Products.Id

    Thanks in advance

    Published by: ZiKaS on December 30, 2008 02:05

    Oops then try like this

    SELECT c.ProductId, p.Name, SUM(c.Amount)
      FROM Collected_Goods c
      JOIN Products p
        ON c.ProductId = p.ProductId
     GROUP BY c.ProductId, p.name
    
  • Why I can't use the nested aggregate function?

    Hello Experts,

    Why I can't use the nested aggregate function? There is not an ora-00979 group by error of expression.

    Oracle Database 11 g Release 11.2.0.4.0 - 64 bit Production

    Select

       SUM (BOX WHEN (KSD_CREATEDATE BETWEEN TRUNC((KSD_CREATEDATE)) AND TRUNC(MIN(KSD_CREATEDATE)) +60) THEN 1  ELSE 0 END) AS col

    DE TABLE_3_4

    GROUP BY STC_FIRMANO

    Thank you

    GROUP BY will manage the SUM function, but the MIN is used incorrectly - use another SELECTION to get it, or work with only with a single line. Otherwise, you could do WITH... get the SUM of SELECT MIN...

  • Rank as aggregate function - help

    Hi all

    I am trying to learn oracle RANK as an aggregate function.

    After that I run under request.

    with temp (No., name, dept, sal) as

    (select 1, 'aaa', 20, 32254 double Union all

    Select 2, 'eee', 20, 45615 double Union all

    Select 3, 'fff', 10, double union all 34645

    Select 4, 'bbb', 30, double union all 45734

    Select 9, 'mmm', 30, 23225 double Union all

    Select 8, "nnn", 20, double union all 43565

    Select 5, 'bbb', 20, double union all 56116

    Select 7, "ccc", 30, double union all 45166

    Select 6, 'ccc', 20, double 75166)

    Select

    Rank (45166) WITHIN GROUP (order by sal) as rank_1,

    Rank (10,45166) WITHIN GROUP (stopped by the Department, sal) as rank_2,

    Rank (45166,2) GROUP WITHIN (order by sal, no.) as rank_3

    temp;

    I get the output voltage:

    rank_1 > 5

    rank_2 > 2

    rank_3 > 5

    Here, how is the value of rank_2 is 2 and rank_3 is 5, I expect that two column should be zero because it does not correspond with the records.

    Please help me understand, how does the row aggregate function is the species.

    Thanks for all help you. Please let me know for further details.

    If possible, any study link to learn the rank of aggregate function.

    Oracle version:

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    PL/SQL Release 11.2.0.3.0 - Production

    "CORE 11.2.0.3.0 Production."

    AMT for Linux: Version 11.2.0.3.0 - Production

    NLSRTL Version 11.2.0.3.0 - Production

    Its says not where is the match - but where it appears in the list - game or not.

    It's your list sorted by dept, sal

    SAL DEPT

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

    10 34645

    32254 20

    20 43565

    20 45615

    20 56116

    20 75166

    30 23225

    45166 30

    30 45734

    10,45166 would appear in the slot 2

  • Pivot table result without aggregate function

    Hi all

    We have the following two tables:

    SQL > select * from class;

    CLASS_ID COURSES

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

    1          1

    2          1

    3          1

    2          2

    3          2

    5          2

    6 selected lines.

    SQL > select * of course.

    COURSE_ID

    ----------

    1

    2

    The customer needs the output in the following format:

    class class class class

    1                    1               2               3

    2                    2               3               5

    I considered using the operator pivot tables however they require the use of an aggregate function, but there is no aggregation only required in the result here - we demand that the output as would a PivotTable?

    Try this.

    WITH class(CLASS_ID,COURSE) AS)

    1.1 DOUBLE UNION ALL SELECT

    SELECT 2.1 DUAL UNION ALL

    SELECT 3.1 FROM DUAL UNION ALL

    SELECT 2.2 DUAL UNION ALL

    3.2 DUAL UNION ALL SELECT

    SELECT DOUBLE 5.2)

    SELECT * FROM)

    SELECT THE COURSE

    CLASS_ID,

    ROW_NUMBER () OVER(PARTITION BY COURSE ORDER BY COURSE) RN

    CLASS)

    PIVOT (MAX (CLASS_ID) for (RN) IN (1 as CLASS1, 2 AS CLASS2, CLASS3 AS 3));

    OUTPUT:

    CLASS CLASS1 CLASS2 CLASS3

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

    1          1          2          3

    2          2          3          5

  • Aggregate functions

    Why aggregate (sum, min) function returns a line (with a null value) even though my there is no lines satisfying the where clause. Pls find the below example

    Select 1 from scott.emp where deptno = 1,011 - no row returned

    Select sum (sal) in the scott.emp where deptno = 1,011 - returns a row with null as a value. The thing is that if there are no rows in the emp table with a floating point number of employee. (Sum) should not have been called.

    Here's how that works:

    http://docs.Oracle.com/CD/E11882_01/server.112/e26088/functions003.htm

    All functions except aggregation COUNT (*), GROUPING , and GROUPING_ID ignore NULL values. You can use the NVL function in the argument of an aggregation function to substitute a value for a null value COUNT and REGR_COUNT never return null, but returns a number or zero. For all the remaining functions of aggregation, If the DataSet has no rows, or contains only rows with NULL values as arguments to the aggregate function, then the function returns the value null.

  • Using java user-defined aggregate function

    Hello

    I'm trying to implement a with Java user-defined aggregate function.

    Here's the Code Javaclass:

    Import Java.util;
    Oracle.sql import. *;
    import java.sql. *;
    import Java.Math.BigDecimal;
    Oracle import. CartridgeServices.*;

    stored the context type

    public class StoredCtx_SecMax
    {
    private BigDecimal b_max;
    private BigDecimal b_secmax;

    public StoredCtx_SecMax()
    {
    b_max = new BigDecimal (0);
    b_secmax = new BigDecimal (0);
    }

    public void SchreibeMax (entry BigDecimal)
    {
    b_max = entry;
    }

    public void SchreibeSecMax (entry BigDecimal)
    {
    b_secmax = entry;
    }

    public BigDecimal LeseMax()
    {
    Return b_max;
    }

    public BigDecimal LeseSecMax()
    {
    Return b_secmax;
    }
    }

    implementation type

    / public class SecondMax_j implements SQLData
    {
    BigDecimal private key;

    Ultimate SUCCESS of static BigDecimal = new BigDecimal (0);
    Final static BigDecimal ERROR = new BigDecimal (1);


    Implement the SQLData interface.

    String sql_type;
    public String getSQLTypeName() throws SQLException
    {
    Return sql_type;
    }

    ' Public Sub readSQL (SQLInput stream, string typeName) throws SQLException
    {
    sql_type = typeName;
    key = stream.readBigDecimal ();
    }

    Public Sub writeSQL (SQLOutput stream) throws SQLException
    {
    stream.writeBigDecimal (key);
    }


    methods of the type that implements ODCITable interface

    public static BigDecimal ODCIAggregateInitialize (oracle.sql.STRUCT [] sctx)
    throws SQLException
    {
    Connection Conn = DriverManager.getConnection("jdbc:default:connection:");

    create a stored environment and store the result set in it
    StoredCtx_SecMax ctx = new StoredCtx_SecMax();

    context of registry stored with cartridge services
    Touch int;
    try {}
    key = ContextManager.setContext (ctx);
    } catch {} (EC CountException)
    error return;
    }

    Create an instance of StockPivotImpl and store the key of
    Object [] impAttr = new Object [1];
    impAttr [0] = new BigDecimal (key);
    StructDescriptor sd = new StructDescriptor ("SECONDMAX_J", conn);
    SCTX [0] = new STRUCT(sd,conn,impAttr);

    return a SUCCESS;
    }

    public BigDecimal ODCIAggregateIterate (BigDecimal input)
    throws SQLException
    {
    retrieve the context stored with the key
    CTX StoredCtx_SecMax;
    try {}
    CTX = (StoredCtx_SecMax) ContextManager.GetContext (Key.intValue ());
    } catch (InvalidKeyException ik) {}
    error return;
    }

    BigDecimal b_max;
    BigDecimal b_secmax;

    b_max = ctx. LeseMax();
    b_secmax = ctx. LeseSecMax();

    If (eingabe.compareTo (b_max) == 1)
    {
    CTX. SchreibeSecMax (b_max);
    CTX. SchreibeMax (input);
    }
    on the other
    {
    If (eingabe.compareTo (b_secmax) == 1)
    {
    CTX. SchreibeSecMax (input);
    }
    }

    return a SUCCESS;
    }

    public BigDecimal ODCIAggregateTerminate (double [] returnValue, int flags) throws SQLException {}

    retrieve the context stored with the key
    CTX StoredCtx_SecMax;
    try {}
    CTX = (StoredCtx_SecMax) ContextManager.clearContext (Key.intValue ());
    } catch (InvalidKeyException ik) {}
    error return;
    }

    returnValue [0] = ctx. LeseSecMax () .doubleValue ();

    CTX = null;

    return a SUCCESS;
    }


    public BigDecimal ODCIAggregateMerge (oracle.sql.STRUCT ctx2) throws SQLException {}


    return a SUCCESS;
    }
    }


    To implement this in the database, I use the following script:

    CREATE OR REPLACE TYPE IMPROG. SECONDMAX_J as object

    (
    INTEGER key,

    public static function ODCIAggregateInitialize (sctx IN OUT SECONDMAX_J)

    Return number
    IN THE JAVA LANGUAGE
    NAME "Java.Math.BigDecimal return from SecondMax_j.ODCIAggregateInitialize (oracle.sql.STRUCT []).
    ,



    Member ODCIAggregateIterate (self IN OUT SECONDMAX_J function,

    Number return value)
    IN THE JAVA LANGUAGE
    NAME "Java.Math.BigDecimal SecondMax_j.ODCIAggregateIterate return (Java.Math.BigDecimal).
    ,



    Member ODCIAggregateTerminate (self IN SECONDMAX_J function,

    Return returnValue NUMBER, flags in number number)
    IN THE JAVA LANGUAGE
    NAME "Java.Math.BigDecimal return of SecondMax_j.ODCIAggregateTerminate (double [], int).
    ,



    Member ODCIAggregateMerge (self IN OUT SECONDMAX_J function,

    CTX2 IN SECONDMAX_J) return number
    IN THE JAVA LANGUAGE
    NAME "Java.Math.BigDecimal SecondMax_j.ODCIAggregateMerge return (oracle.sql.STRUCT)".
    );
    /

    CREATE OR REPLACE FUNCTION F_SECONDMAX_J (NUMBER value) RETURN NUMBER

    PARALLEL_ENABLE

    AGGREGATION to the HELP of SecondMax_j;
    /



    When I now use the F_SECONDMAX_J function.

    "(Columnname) select F_SECONDMAX_J from Tablename t;

    It works perfectly and shows me the right result.

    Then I tried to implement the fusion part as follows:

    public BigDecimal ODCIAggregateMerge (oracle.sql.STRUCT ctx2) throws SQLException {}

    BigDecimal Key2 = null;

    BigDecimal ctx_max = null;
    BigDecimal ctx_secmax = null;

    BigDecimal ctx2_max = null;
    BigDecimal ctx2_secmax = null;

    retrieve the context stored with the key
    StoredCtx_SecMax ctx = null;
    try {}
    CTX = (StoredCtx_SecMax) ContextManager.GetContext (Key.intValue ());
    } catch (Exception e) {}

    }

    Object [] impAttr = ctx2.getAttributes ();

    Key2 = (BigDecimal) impAttr [0];

    retrieve the second context stored using the key2
    StoredCtx_SecMax ctx_sec = null;
    try {}
    ctx_sec = (StoredCtx_SecMax) ContextManager.GetContext (key2.intValue ());
    } catch (Exception e) {}

    }

    ctx_max = ctx. LeseMax();
    ctx_secmax = ctx. LeseSecMax();

    ctx2_max = ctx_sec. LeseMax();
    ctx2_secmax = ctx_sec. LeseSecMax();

    If (ctx2_max.compareTo (ctx_max) == 1)
    {
    If (ctx2_secmax.compareTo (ctx_secmax) == 1)
    {
    CTX. SchreibeSecMax (ctx2_secmax);
    }
    on the other
    {
    CTX. SchreibeSecMax (ctx_max);
    }
    CTX. SchreibeMax (ctx2_max);
    }
    on the other
    {
    If (ctx2_max.compareTo (ctx_secmax) == 1)
    {
    CTX. SchreibeSecMax (ctx2_max);
    }
    }

    return a SUCCESS;
    }

    When I now try the function

    "Select / * + parallel (t 4) * / F_SECONDMAX_J (Columnname) of Tablename t;

    I always get a pointer NULL-error because the variable key2 is always 0 and therefore the functions LeseMax() and LeseSecMax()
    in the second context 'ctx_sec' produce this error NULL pointer.

    To me it seems that each parallel process is the construction of an additional class for himself, which is not available are my main process.

    My question is now how it is possible to access the context of stored of the parallel process, so that I can combine the results.

    Thank you very much

    Josef

    Hi Josef,

    just guess that I've never implemented with java user-defined aggregate functions or support parallel execution:

    Maybe you have to implement ODCIAggregateWrapContext?

    + Application of ODCIAggregateWrapContext is optional. It should be implemented only when the external memory is used to hold the aggregative, context and the user-defined aggregate is evaluated in parallel (which is, stated as PARALLEL_ENABLE). If the user-defined aggregate is not evaluated in parallel, ODCIAggregateWrapContext is not necessary. +
    [http://docs.oracle.com/cd/B12037_01/appdev.101/b10800/dciaggfns.htm#i1004974]

    Report it to progress and success.

    Good luck
    Rainer Stenzel

  • Performance of the aggregate function.

    Hello

    Version 11.2.0 Oracle

    I'm not a PL/SQL Developer, however, I decided to write this aggregate out function. Oracle has it but I tried TimesTen database in memory that does not yet function stddev.

    The code is as follows:
    CREATE OR REPLACE TYPE MyStddevImpl AS OBJECT
    (
      v_power NUMBER, -- sum(power^2) of the column
      v_sum   NUMBER, -- average value
      v_iteration NUMBER, -- count(1)
    
      STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT MyStddevImpl)
        RETURN number,
    
      MEMBER FUNCTION ODCIAggregateIterate(self IN OUT MyStddevImpl,
        value IN number)
        RETURN number,
    
      MEMBER FUNCTION ODCIAggregateTerminate(self IN MyStddevImpl,
        returnValue OUT number, flags IN number)
        RETURN number,
    
      MEMBER FUNCTION ODCIAggregateMerge(self IN OUT MyStddevImpl,
       ctx2 IN MyStddevImpl)
       RETURN number
    );
    /
    show error
    
    CREATE OR REPLACE TYPE BODY MyStddevImpl
    AS
    STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT MyStddevImpl)
    RETURN number
    AS
    BEGIN
      sctx := MyStddevImpl(0,0,0);
      RETURN ODCIConst.Success;
    END;
    
    MEMBER FUNCTION ODCIAggregateIterate(self IN OUT MyStddevImpl, value IN number)
    RETURN number
    AS
    BEGIN
      self.v_sum   := self.v_sum + value;   -- used to get averages
      self.v_power := self.v_power + power(value,2);
      self.v_iteration := self.v_iteration + 1;  -- total  number of rows
      return ODCIConst.Success;
    END;
    
    MEMBER FUNCTION ODCIAggregateTerminate(self IN MyStddevImpl, returnValue OUT
                                           number, flags IN number)
    RETURN number
    AS
      v_avg number;
    BEGIN
      v_avg :=  self.v_sum/self.v_iteration;   -- this is the average value
       -- sqrt((sum(power(amount_sold,2))-(count(1)*power(avg(amount_sold),2)))/(count(1)-1))
      returnValue := sqrt((self.v_power - (self.v_iteration * power(v_avg,2))) /(self.v_iteration-1));
      RETURN ODCIConst.Success;
    EXCEPTION
      WHEN VALUE_ERROR
      THEN
        RETURN ODCIConst.error;
    END;
    
    MEMBER FUNCTION ODCIAggregateMerge(self IN OUT MyStddevImpl, ctx2 IN
    MyStddevImpl)
    RETURN number
    AS
    BEGIN
     RETURN ODCIConst.Success;
    END;
    END;
    /
    show error
    CREATE OR REPLACE FUNCTION MyStddev (input NUMBER)
    RETURN number
    AGGREGATE USING MyStddevImpl;
    /
    show error
    exit
    Now, it works very well and returns the results OK.
    select mystddev(amount_sold) AS "My standard deviation", stddev(amount_sold) from sales;
    
    My standard deviation STDDEV(AMOUNT_SOLD)
    --------------------- -------------------
                259.78049           259.78049
    The problem I have is that it takes 10.5 sec to run
     select mystddev(amount_sold) from ssdtester.sales;
    
    MYSTDDEV(AMOUNT_SOLD)
    ---------------------
               273.172955
    
    Elapsed: 00:00:10.48
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1047182207
    
    ----------------------------------------------------------------------------
    | Id  | Operation          | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |       |     1 |     5 |  1324   (1)| 00:00:16 |
    |   1 |  SORT AGGREGATE    |       |     1 |     5 |            |          |
    |   2 |   TABLE ACCESS FULL| SALES |  1000K|  4882K|  1324   (1)| 00:00:16 |
    ----------------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
           4965  consistent gets
              0  physical reads
              0  redo size
            558  bytes sent via SQL*Net to client
            524  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processed
    Is there any scope to improve this code. Specifically can I use avg function in the code instead of working on myself?

    Thank you

    Mich

    Published by: Mich Talebzadeh on February 13, 2012 12:20

    >
    May I ask what is the purpose of the model of aggregate by Oracle functions if it will be row-by-row and he's not going to be effective.
    >
    Oracle online the way you did in your function.

    Your function, using the technology of data cartridges, will be called by Oracle for EACH LINE it reads each line corresponding to your query. This requires a change in context of SQL, PL/SQL, and then back to SQL. It is a very expensive operation in terms of time.

    The Oracle SQL engine is not that way for aggregate functions it provides. The functionality of data cartridge is provided to allow developers to extend Oracle to provide features that Oracle is not provde all. Use of these functions of cartridge cases, you develop may not be 'slow' because there is no equivalent Oracle to compare to. You can use the features of your new cartridge or do you without; you have no other choice.

    You should always use SQL from Oracle to do the job unless you are doing something that can be done in SQL. Then you should use of Oracle PL/SQL features (for example in BULK of treatment) to do the more complex things. Finally if you need to do something just that Oracle cannot do with out-of-the-box SQL or PL/SQL, then you turn cartridge data, custom Java or C code or other external solutions which you can interface to Oracle.

    The order of the solutions, you should look at is:
    1 SQL
    2 PL/SQL
    3 Java/C external
    4 data cartridge

    He explains a little better? (and it is frankly win again in a minute or two!)

  • analytical function and the aggregate function

    What are the analytical function and the aggregate function. What is the difference between them?

    Hello

    Analytic Functions : -.

    Analytical functions calculate a value of aggregation based on a group of lines. They differ from aggregate functions because they return several rows for each group. The Group of rows is called a window and is defined by the analytic_clause. For each line, a sliding window of lines is defined. The window determines the range of lines used for the calculations for the current line. Window sizes can be based on a physical number of rows or a logic as the time interval.
    Analytical functions are the last set of operations performed in a query with the exception of the last ORDER BY clause. Every joint and every WHERE, GROUP BY and HAVING clauses are met before the analytical functions are handled. As a result, analytic functions can only appear in the select list or the ORDER BY clause.
    Analytical functions are commonly used to calculate cumulative aggregates, moving, centered and considered.

    Aggregate functions : -.

    Aggregate functions return a line of single result based on the groups of lines, rather than on the unique lines. Aggregate functions can appear in selection lists, as well as in the HAVING and ORDER BY clauses. They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle Database splits the rows in a table when asked or seen in groups. In a query that contains a GROUP BY clause, the select list items can be aggregation functions, GROUP BY constant expressions or expressions involving one of them. Oracle applies the functions of aggregation for each group of rows and returns a single result for each group line.
    If you omit the GROUP BY clause, Oracle then applies any aggregate functions in the select list for all rows in the table queried or the view. You use aggregate functions in the HAVING clause to eliminate groups of the output based on the results of aggregate functions, rather than the values of the individual lines of the queried table or view.

    Let me know if you feel any problem understanding.
    Thank you.

    Published by: varun4dba on January 27, 2011 15:32

Maybe you are looking for

  • Need drivers for Satellite 1110 (PS111E-0039Y)

    Hello I have this model laptop, which is quite old now, but I can't seem to find the drivers for it on the Toshiba site, maybe the drivers for my model come under another name? If anyone can help me it would be much appreciated.

  • Update of BIOS that need for the older ThinkPad T42p

    So my friend was extremely difficult to get the acquired IP on its ThinkPad T42p running XP Pro... In fact the gateway not also acquires.  I see the answer in another question years ago.  Or a possible answer anyway.  But I can't find the IBM BIOS Fl

  • Suppliers to change blackBerry Smartphones

    I think to switch carriers to Sprint, but I was told that I would not be able to switch my blackberry for them... Is this true?  I have to buy a new blackberry just to change provider or my blackberry can be reconfigured to work with Sprint?

  • two or more frame with fabric in cluster interconnect

    Hi experts I just want to confirm the proper way to connect two or more UCS 5108 chassis with a pair of interconnect fabric 6120xp clustered. I though that they should connect like this: the tissue Extender first of all chassis must connect on interc

  • How to go beyond the limit of age to music explicit in Xbox Music

    original title: Xbox Music Salvation of problems playing music from the xbox, explicit cannot be played because of me not being is not old enough even though I am 23. How can I fix? Thank you