Creating Group Functions

I wish to create package functions that I can use in normal SQL to calculate on the values of a column from multiple rows, like max() does.
I am not sure if this is even possible in Oracle 8i or 9i.
Specifically, I want three functions:
- FIRST( value ) that returns the first value from the grouped rows
- LAST( value ) that returns the last value from the grouped rows
- CONCAT( value ) that returns a string that is the concatenation of the value of each of the grouped rows
Imagine a table with two columns, GRP and CODE, with the following values
GRP CODE
1 A
1 B
2 A
2 B
2 C
If my SQL were
SELECT grp,
, pkg.first(code) first
, pkg.last(code) last
, pkg.concat(code) str
FROM TABLE
GROUP BY grp;
I would get the output
GRP FIRST LAST STR
1 A B AB
2 A C ABC
Is this possible?
Thanks in advance,
Chris Elvart of SBC

User defined aggregate functions are definitely available in Oracle 9i. See
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:5269744394385
or search for User defined aggregates on asktom.oracle.com
The functions you want are more poorly defined - first and last depend on the order the rows are retrieved, as does the concat.
You should also consider analytic functions, which certainly support the first and last operations, with defined order within the group. See FIRST_VALUE and LAST_VALUE in the SQL reference manual. Analytic functions require Enterprise Edition.

Similar Messages

  • Is there a way to create a group function?

    I want to create a group function like max,sum,count for a
    pl/sgl function. This group function will be something like max
    (let's call it max2) but it will return the second highest value
    of a group.
    Any ideas anyone?

    Since you might not completely understand what i mean i give you
    this example:
    the ranktest table is:
    A     B     C
    a     b     1
    a     b     1
    a     b     5
    a     b     12
    a     b     7
    b     c     1
    b     c     3
    b     c     2
    b     c     4
    b     c     8
    c     d     4
    c     d     3
    c     d     6
    c     d     1
    c     d     2
    c     d     12
    c     d     7
    c     d     3
    a     b     5
    c     d     12
    SELECT A,B,C
    FROM (
    SELECT A,B,C, dense_rank() over (
    PARTITION BY A,B
    ORDER BY C desc) AS sorted_c
    FROM ranktest a)
    WHERE sorted_c = 2
    -- this way we get the c column to have the 2nd highest value
    -- for each set of A and B
    --RESULT
    a b 7
    b c 4
    c d 7
    SELECT a.A,a.B, sum(a.C), count(a.C)
    FROM ranktest a
    GROUP BY a.A,a.B
    -- this way we get the group functions to work on two of the
    -- columns
    --RESULT
    a b 31 6
    b c 18 5
    c d 50 9
    The result that i want is
    a b 31 6 7
    b c 18 5 4
    c d 50 9 7
    My question is can this be done in a single select statement. I
    know it can be done with an intermediate step (of the creation
    of a temp table).
    Plus i would like to ask the experts, would a single select
    statement be more efficient?
    NOTE: the tables and data are just test data. The real tables
    and data and the select query are different. The real ranktable
    has about 1 million records. That's why i am worried about
    efficiency.

  • How to create a user_defined grouping function?

    I need to create a user_defined function and use it in my sql statement.
    I know the user_defined function can be used anywhere the build-in function can be used.In oracle,there are grouping functions.
    How can I create a user_defined grouping function and I can use it in my sql statement.

    Hi, can you explain exactly what you are trying to achieve:
    SQL> create or replace function my_func return number as
      2  begin
      3    return 5;
      4  end;
      5  /
    Function created.
    SQL> select deptno,my_func(),max(sal)
      2  from emp
      3  group by deptno,my_func()
      4  /
        DEPTNO  MY_FUNC()   MAX(SAL)
            10          5       5000
            20          5       3000
            30          5       2850

  • How to create a grouping function

    How to create a grouping function ?? For example, can I create a function:myavg and I can use it like :
    select col_a, col_b, myavg(col_c)
    from table_a
    group by col_a, col_b
    Thanks in advance !!
    null

    I don't think that you can create a grouping function in Oracle, but you can create a function and use it in Select statments.
    null

  • How to create a function group (step by step)?

    I would like to know how to create a function group, step by step.
    step 1 do....
    step 2 do ...

    also,
    Go to SE80 for creating a function group.
    Also have a look at below link.
    http://help.sap.com/saphelp_nw04/helpdata/en/d1/801ef5454211d189710000e8322d00/frameset.htm
    i.e.
    TCODE - SE80
    Choose - Function Group ,it wil ask to create..
    Create and then activate..give a name starting with Z .
    Other way is go to tcode SE37.
    In the menu,there is a option in sub menu for creating function group.
    Just create it by clicking that and give names starting with z or y.

  • Wich transaction I use to create a functions group? Diferent to SE80

    Hi,
    I need to create a functions group and one autorizations group, but I don't want to use the transaction SE80. Someone help me? What transaction I can use?
    Thanks,
    Maria C.

    Hi Maria,
    just out of curiosity: Why not SE80 (i use it for everything including dictionary or transports)?
    Regards,
    Clemens

  • Function module to the newly created 'Z' function group

    I am facing a strange issue when I am trying to reassign the existing 'Z' Function module to the newly created 'Z' function group. The system gives an error as 'Modified Function Modules may not be reassigned'.
    Could anyone face this issue before or if you have any suggestion on this, please let me know.
    Thanks in Advance.

    Hi,
    Check this steps:
    1) The FM to be moved is activated.
    2) Both new Z-FG and the previous FG are activated.
    3) You are not editing the the FM you are trying to move.
    4) If you are in the Object List view (se80 view) goto : Utilities->Update Navigation Index
    Guess that should take care of the issue!!
    Cheers,
    Lakshmiraj
    Reward if useful

  • How to Create a Functiona Query

    Dear all,
    Could any body detail me how to create a functional query. Please give me step by step procedure to create a query.
    I would appreciate if you can give me a material with screen shots. Your help will be highly appreciated.
    Thank you
    Raghu Ram

    Hi Raghu,
               Go through this document it may help you,
    SAP Query
    Step 1: Create User Group – SQ03
    Follow the menu path – SAP Menu > Tools > ABAP Workbench > Utilities > SAP Query > User Groups
    a) In the User Group: Initial Screen -
    Enter User Group Code (self named)
    Click on “Create”
    b) In the User Group (Code): Create or Change pop-up window -
    Enter the User Group Description
    Click on “Save”
    c) In the Create Object Directory Entry pop-up window -
    Ö Click on “Local Object” button
    Ö The User Group Created is saved
    Step 2: Create User Group – SQ02
    Follow the menu path – SAP Menu > Tools > ABAP Workbench > Utilities > SAP Query > Infosets
    a) In the Infoset: Initial screen -
    Ö Enter Infoset Code (self named)
    Ö Click on “Create” button
    b) In the Infoset: Title & Database Screen -
    Ö Enter Description of Infoset in the “Name” field
    Ö In the Data Source selection: Select appropriate Data Source by clicking
    the radio button
    · Source the data can either be multiple tables OR single table. There are 4
    options for the user to select from
    Ö Click on “Continue” (Enter)
    Ö Selected Table gets displayed in the Infoset : Initial Screen
    c) In the Infoset: Initial Screen -
    Ö Click on “Insert Table” button
    d) In the Add Table Screen -
    Ö Enter the Table Name that is to be inserted
    Ö Click “Continue” (Enter)
    e) In the Infoset: Initial Screen -
    Ö Click on “Back” button
    f) In the Field Group Defaults pop-up Screen -
    Ö Select Appropriate Field Group Option by clicking on the radio button
    · there are 3 options to select from. User can select Empty Field Group
    option and then select the data fields in the steps explained further.
    Alternatively, User can select an option which clubs all the data fields
    From all the tables selected
    Ö Click “Continue” (Enter)
    g) In the Change Infoset Screen -
    · the left side lists the Tables that the user has selected in step b, c & d
    · In the right side, system creates field groups, one for each table listed in the
    left part. Initially, the Field Groups are empty
    Ö In the Left Side, Click on the arror next to the table name and expand and
    display all the data fields in the table
    Ö Select a Data Field from the expanded Table view, which is to be added to
    the Field Group on the right.
    Ö Select a Field Group on the right side, in which Data Field selected above
    has to be added
    Ö Right click on the Data Field selected and click on “Add Field to Field Group”
    Ö The selected Data Field gets added to the selected Field Group, which is
    indicated by an arrow next to the Field Group.
    Ö Carry out the steps above to add the required Data Fields to the respective
    Field Groups
    Ö After transferring Data Fields to Field Groups click on “Generate” button
    h) In the Create Object Directory Entry Screen -
    Ö Click on “Logical Object” button to generate the infoset
    i) In the Change Infoset Screen -
    Ö Click on the “Back” button
    j) User is taken back to the “Infoset: Initial Screen” which displays the
    created Infoset record.
    Step 3: Assigning User Group to Infoset – SQ03
    Follow the menu path – SAP Menu > Tools > ABAP Workbench > Utilities > SAP Query > User Groups
    a) In the User Groups: Initial screen -
    Ö Enter the User Group for which Infoset is to be assigned
    Ö Click on “Assign Users & Infosets” button
    b) In the User Group: Assign Users Screen -
    Ö Click on “Assign Infosets” button
    c) In the next screen, select the Infoset, which needs to be attached to the
    User Group selected
    d) Click on the “Back” button
    Step 4: Creating Query – SQ00 & SQ01
    Follow the menu path – SAP Menu > Tools > ABAP Workbench > Utilities > SAP Query > Queries
    a) In the “Query from User Group: Initial Screen -
    Ö Click on “Other User Groups” button
    Ö In the pop-up screen, User Groups, select the User Group for which the
    Query has to created. The pop-up screen closes.
    Ö Enter the Query Code in the “Query” field
    Ö Click on “Create” button
    b) A pop-up screen “Restrict Value Range” shows the list of Infosets assigned to
    the User Group
    Ö Select the Infoset for which Query has to be created
    c) In the new screen Create Query: Title, Format -
    Ö Enter the Description of the Query in the “Title” field
    Ö Click on the “Next Screen” button
    d) In the Select Field Group” screen
    Ö Select the Field Groups from which Data Fields have to be selected for the
    Output
    Ö Click on the “Next Screen” button
    e) In the “Select Field” screen -
    Ö Select the Data Fields, which should appear in the output
    Ö Click on the “Next Screen” button
    f) In the Screen “Selection” -
    Ö Select the Data Fields for the selection criteria
    Ö Enter the sequence in which the selected fields would appear in the input
    screen of the query.
    Ö Define if the selection criteria should be Single Value or Multiple Value Range
    by clicking in the appropriate check boxes
    Ö Click on the “Basic List’ button
    g) In the screen “Query Layout Design” -
    Ö Select the Output Fields from the Data Fields section by clicking the
    appropriate check box
    Ö Change the sequence of Data Field columns if required
    Ö Click on the “Test” button to test the query created
    h) On the “Test Query” screen click “Continue”
    i) In the “Query” screen -
    Ö Enter the selection criteria
    Ö Click on “Execute” button
    j) Save the Query created
    Step 5: Running the Query – SQ00
    a) In the Query From User Group: Initial screen –
    b) Click on “Other User Groups” button to select the User Group
    c) In the pop-up screen “User Groups” select the User Group
    d) System lists all the queries created for the User Group
    e) Select the appropriate Query
    f) Click on “Execute” button
    g) User is taken to the Selection screen
    h) Enter the selection criteria and click on “Execute” button
    i) System runs the query and gives the output
    Regards,
    Murali.

  • I can no longer create groups that contain subgroups??

    Folks,
    I used to be able to create a group, drag existing subgroups and individuals into that group, and send emails to all by simply typing the group name into Mail.
    I can no longer do this.
    Now, to be precise. I can create what looks like a group and by dragging existing subgroups into it -- but it no longer functions as a group. When I type the name or drag it (from Contacts) into the "To:" line of Mail, the only thing that shows up are the top-level individuals (i.e., those who I put dragged in as individuals, not those who are only in one of the subgroups that I dragged in).
    If I create a group that only has subgroups (i.e., no individuals at the top level, only individuals who are members of one or more of the included subgroups) then after I type the group name in the "To:" line of mail, the group name dissappears, and nothing appears in Mail's "To:" line.
    The actual behavior is a little more subtle than this but the bottom line is that I can no longer create groups with subgroups.
    Help!!

    Hi,
    If you list the steps you have taken to fix this it will help.
    Have you tried using the File > New Event menu item?
    Have you logged the user account out/ restarted the computer?
    Have you tried un-syncing any accounts synced to Calendar?
    Is there some change made to the computer that may have triggered this?
    Best wishes
    John M

  • HOW to create a function module ..... in abap

    pls lemme know ............the steps and procedure in creating a function module

    hi GURU,
    To Create Function Module first you need to create Function Group and Activate it.
    First go to SE37 then
    1. Goto>Function Groups>Create Group.
        Here you need to give Group name and Short Text of group.
    2. After Creating Function Group again:
       Goto>Function Groups>Change Group.
       Enter your Function Group name and press enter.
       a dialog box will appear where you can see its status is inactive.
    So, press <b>Main Program</b> Button and activate your function group.
    NOW you Function Group is activated and and you can create Function Module in it.
    Now Enter your Function Module Name and press create then
    give your Function Group name in respective place and press enter.
    Now you can give your Import/Export parameters and and if you want to use TABLES give it in TABLE section only not in Import/Export Parameters.
    Then Write Logic in Source Code Tab and Activate it.
    if you have any qury regarding it Pls let me know.
    Reward if Useful.
    Sachin.

  • Error creating RFC function /BODS/ABAP_RUN : RFC_ABAP_EXCEPTION-(Exception_Key: FU_NOT_FOUND, SY-MSGTY: E, SY-MSGID: FL,

    Hi Expert,
    I try to execute job on development system but he showed error "Error creating RFC function </BODS/ABAP_RUN>: <RFC_ABAP_EXCEPTION-(Exception_Key: FU_NOT_FOUND, SY-MSGTY: E, SY-MSGID: FL, SY-MSGNO: 046, SY-MSGV1: /BODS/ABAP_RUN)>. Notify Customer Support.
    In ECC I use ABAP execute option as Execute Preloaded. How to solve this problem? Thanks for your advise.

    Dear,
    have you checked in EXX wether the function group /BODS/BODS is available?
    Some notes here:
    You will have to have to import the new ABAP Program group "BODS/BOS" which you can find in the local install folder...
    Here are some details:
    Installing Functions on the SAP Server
    SAP BusinessObjects Data Services provides functions that support the use of the ABAP, BAPI, and
    IDoc interfaces on SAP servers. You will need some or all of these functions in the normal operation
    of the software in an SAP environment. These functions perform such operations as dynamically loading
    and executing ABAP programs from Data Services, efficiently running preloaded ABAP programs,
    allowing for seamless metadata browsing and importing from SAP servers, and reporting the status of
    running jobs. Some of these functions read data from SAP NetWeaver BW sources.
    You must upload the provided functions to your SAP server in a production environment. It is
    recommended that you always upload the functions to your SAP server whether you are in a
    development, test, or production environment. The functions provide seamless integration between
    Data Services and SAP servers.
    The default installation places two function module files for SAP servers in the ...\Data
    Services\Admin\R3_Functions\transport directory. You then upload these files to SAP servers
    using the SAP Correction and Transport System (CTS) or manually. Using CTS allows for version
    control as the functions evolve across releases.
    The installation provides two versions of transport files (depending on the server version you are using)
    to install the functions on the SAP server. To obtain the names of the latest transport files for installing
    or upgrading these SAP server functions, see the readme.txt file
    And I've found those files and text files in the local install folder....in:
    Program Files\SAP BusinessObjects\Data Services\admin\R3_Functions
    (that's where I've installed it).
    There you'll find some descriptive txt as how to proceed.
    After installing, it might happen that the executing user is missing some authorizations.
    Here my authorizations team helped me by tracing the user and then adding the necessary rights.
    Sure hope this will help you.
    Notes to check:
    see SAP Note 1919255
    Note 1916294

  • Nested group function without group xmlagg

    I am getting nested group function without group by xmlagg when using the xmlagg function inside another xmlagg function. Find the table structure and sample data here,
    CREATE TABLE "TEST_TABLE"
       ("KEY" NUMBER(20,0),
        "NAME" VARCHAR2(50 ),
        "DESCRIPTION" VARCHAR2(100 )
       Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (1,'sam','desc1');
       Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (2,'max','desc2');
       Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (3,'peter',null);
       Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (4,'andrew',null);
    select
            XMLSerialize(document
            xmlelement("root",
             xmlagg(
               xmlelement("emp"          
               , xmlforest(Key as "ID")          
               , xmlforest(name as "ename")
               , xmlelement("Descriptions", 
               xmlagg(
                  xmlforest(description as "Desc")
           ) as clob indent
           ) as t   
          from test_table;Then i removed the xmlagg function from the above select query and used xmlelement instead
      select
            XMLSerialize(document
            xmlelement("root",
             xmlagg(
               xmlelement("emp"          
               , xmlforest(Key as "ID")          
               , xmlforest(name as "ename")
               , xmlelement("Descriptions",            
                  xmlforest(description as "Desc")
           ) as clob indent
           ) as t   
          from test_table;This is working fine, but xml created with empty elements for Descriptions element for key 3 and 4 which has null values. I need don't need Descriptions element in the xml when it has null value. Please help me to resolve this.

    You can do it with a correlated subquery :
    SQL> select xmlserialize(document
      2           xmlelement("root",
      3             xmlagg(
      4               xmlelement("emp"
      5               , xmlforest(
      6                   t.key as "ID"
      7                 , t.name as "ename"
      8                 , (
      9                     select xmlagg(
    10                              xmlelement("Desc", d.description)
    11                              order by d.description -- if necessary
    12                            )
    13                     from test_desc d
    14                     where d.key = t.key
    15                   ) as "Descriptions"
    16                 )
    17               )
    18             )
    19           ) as clob indent
    20         )
    21  from test_table t;
    XMLSERIALIZE(DOCUMENTXMLELEMEN
    <root>
      <emp>
        <ID>1</ID>
        <ename>sam</ename>
        <Descriptions>
          <Desc>desc1_1</Desc>
          <Desc>desc1_2</Desc>
          <Desc>desc1_3</Desc>
        </Descriptions>
      </emp>
      <emp>
        <ID>2</ID>
        <ename>max</ename>
        <Descriptions>
          <Desc>desc2_1</Desc>
          <Desc>desc2_2</Desc>
          <Desc>desc2_3</Desc>
        </Descriptions>
      </emp>
      <emp>
        <ID>3</ID>
        <ename>peter</ename>
      </emp>
      <emp>
        <ID>4</ID>
        <ename>andrew</ename>
      </emp>
    </root>
    Or an OUTER JOIN + GROUP-BY :
    select xmlserialize(document
             xmlelement("root",
               xmlagg(
                 xmlelement("emp"          
                 , xmlforest(
                     t.key as "ID"
                   , t.name as "ename"
                   , xmlagg(
                       xmlforest(d.description as "Desc")
                       order by d.description -- if necessary
                     ) as "Descriptions"
             ) as clob indent
    from test_table t
         left outer join test_desc d on d.key = t.key
    group by t.key
           , t.name
    ;Edited by: odie_63 on 11 juil. 2012 14:54 - added 2nd option

  • PL/SQL: ORA-00934: group function is not allowed here

    Hi,
    I am writing a PL/SQL procedure. The structure is like :
    SET SERVEROUTPUT ON;
    CREATE OR REPLACE Procedure abc
    IS
    v_total_ip_rec number(14);
    v_total_op_rec number(14);
    v_total_rec number(14);
    BEGIN
    SELECT SUM (CASE
    WHEN <condition 1>
    THEN 1
    ELSE 0
    END
    ) into v_total_ip_rec,
    SUM (CASE
    WHEN <condition 2>
    THEN 1
    ELSE 0
    END
    ) into v_total_op_rec,
    SUM (1) into v_total_rec
    FROM A,B
    WHERE A.Col1=B.Col1;
    EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    END;
    When I run this procedure it gives me following error:
    "PL/SQL: ORA-00934: group function is not allowed here"
    Anybody have any idea?
    Any help would be appreciated.
    Thanks.

    Hi Arunkumar ,
    I think you don't need subquery.
    Regards Salim.
    Or.
    SELECT COUNT (CASE
                     WHEN <condition 1>
                        THEN 1
                  END) v_total_ip_rec,
           COUNT (CASE
                     WHEN <condition 2>
                        THEN 1
                  END) v_total_op_rec,
           COUNT (1) v_total_rec
      FROM a, b
    WHERE a.col1 = b.col1

  • Can't find a self created group during add group in reporting

    The Operations manager that i am using si 2012. First let me describe what I have done.
    In "Authoring", I have created a new group and added two object which is a Windows server 2012 into the group. Then, I tried to use the Windows Server 2012 report template in "Reporting" to create a performance report. After opened the
    report template, there is an "Add Group" button to choose a group of server which need to run the performance report. I have tried to search that created group using its name with or without the % wildcard. Unfortunately, I failed to search my group.
    I have tried to use just only the % wildcard to retrieve all entries. I still failed to find my group from the list.
    The next thing I did is connect to the Operation manager's database, that is the OperationManagerDW database. From my understanding, all entries of object should be stored in the dbo.managedentity table. I have tried to look up the table which display name
    is my create group but still fail. My created group does not have an entry in the table.
    So, my question is what I have missed to make my group searchable in creating report during add group?
    Thanks a lot.

    Dear Xin Guo,
    I have tried your SQL statement and the return is same as what I have experienced when using the Add Group function when running report. That is, my custom group created in the Authoring cannot be seen. 
    I have also checked the OM log but no error being noticed. It seems to be the tables which store the Group created in Authoring is not synchronize with the entries in the ManagedEntity tables. 
    It this a known behavior or my process on creating a custom group in Authoring is not correct? Actually, I followed the steps as described in Microsoft website. The steps is very straight forward and it is not easy
    miss anything.
    Looking forward for your further advise in working out on this. Since I want to generate reports for a group of computers by using the custom group instead of select all computers one by one every time when I run a
    report. 
    Thanks a lot in advance.

  • Creating a function module in background

    Hi
    Is there any way by which if i want to create any function module in background .
    like in any screen i have created a button  and on pressing that button a blank fucntion module should be created in the function group that is already created .
    waiting for your reply.

    Hello, Ashish;
    When you mention a "Function Module" where are you trying to create it? The report designer? A .NET form?
    What version of Crystal Reports are you using?
    What version of Visual Studio .NET?
    What requirements are you trying to meet?
    Elaine

Maybe you are looking for

  • Report in Forms9i

    In forms 6/6i I can launch a report in HTML mode working in web mode and I wanted to lauch it in PDF, that PDF file was stored in the server. Can I launch a report in PDF mode in Forms9i to print it from the browser? Joel Pérez

  • New 60 gig video ipod   Which case to buy?

    I want a case that has some cushion on the inside or is at least tight enough not to rub the ipod. I also want the screen and wheel protected. Any suggestions?

  • Browse by Release Date

    Hello. I've always experienced a very frustrating problem when browsing apps by Release Date on my iPad. Steps are as follows: 1. Browse apps by release date. 2. Scroll through several pages, then touch an app to view details. 3. Touch the 'back' arr

  • Itunes doesn't startup with iPod

    Usually when I connect my Touch (2 gen) to a USB port on the iMac, iTunes fires up if not already running. Recently this has failed to happen. iTunes will recognize the Touch if it is already running, but otherwise nothing happens if it is not. This

  • Same application that works on different database user

    I have an application wiritten in htmldb 1.6 taht create areport on a table. But on the database i have a lot of different user (each table of each user have different information) Can i use the same application with connection on differnet user??? T