GROUP BY without GROUP functions

I red in Oracle university book that you can use the GROUP BY clause without using a group function in the SELECT list, can someone give me an example using the popular table 'employees' OR 'departments'

I cannot think of why or how such a query would be useful except it you selected the same column you grouped on you would be doing the equilivent of a select distinct:
UT1 > select deptno from emp group by deptno;
DEPTNO
10
20
30
HTH -- Mark D Powell --

Similar Messages

  • ORA-00978  without group function

    I've experienced a strange problem with oracle 11g.
    I've retrieved the oracle exception ORA-00978 even if there was no group function in my query.
    I supposed was a problem in the optimizer so I rebuild the tables statistics, after that the query was execute successfully.
    Does anyone has an idea what the problem is?
    Is possible that a bug exists in the 11g optimizer?
    My oracle version is:
    Oracle Database 11g Enterprise Edition 11.1.0.6.0 64bit Production
    the query i tried is:
    SELECT *
    FROM TBCALENDAR Cal,
    VWCALENDARACTIVITY CA,
    VWSE R,
    TBSCHEDULERPARTITION P,
    TBREGION REG,
    TBRESOURCE RES ,
    TBZIPCITY z
    WHERE Res.id=Cal.RESOURCE_ID
    AND R.RESOURCE_ID=RES.ID
    AND Cal.ACTIVITY_ID=CA.ID
    AND CA.SCHEDULING=1
    AND Cal.SCHEDPARTITION_ID IN
    (select item.PARTITION_ID
    from tbidcprofile prof,
    tbidcpartitem part,
    tbschedpartitem item
    where prof.USERPROFILE_ID=4
    and prof.IDCPARTITION_ID=part.PARTITION_ID
    and part.BUSINESSUNIT_ID=item.BUSINESSUNIT_ID
    and part.REGION_ID=item.REGION_ID )
    AND TRUNC(Cal.START_DT)=trunc(sysdate)
    AND P.ID=Cal.SCHEDPARTITION_ID
    AND REG.ID(+)=Cal.WORKREGION_ID
    AND Z.GEOLOCATION_ID(+)=Cal.HOMEGEOLOC_ID;
    VWCALENDARACTIVITY and VWSE are two views, but I can select from them without any problem.
    I've also tried to remove one view at a time an the error occurs only when the query uses both view at the same time.
    Thanks
    Renzo

    user479513 wrote:
    VWCALENDARACTIVITY and VWSE are two views, but I can select from them without any problem.
    I've also tried to remove one view at a time an the error occurs only when the query uses both view at the same time.
    What are the views definition ?
    Nicolas.

  • 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

  • Need to take rownum highest value without using grouping functions

    Hi
    I want to display highest value in the rownum column and customer details without using grouping functions like max or count
    this below query gives me all rownum values and customer details
    SELECT ROWNUM ROWNUM_1, CUSTOMER_NO, CUSTOMER_NAME, CUSTOMER_DOJ, CUSTOMER_MOBILENO FROM CUSTOMER;
    can any one help me.

    The above query won't work as it's missing "from" cluase in the inner select statement.
    And even if corrected it willl print rownum values thrice: value "1",max_rownum, max_rownum followed by customer details.
    Below is the simple query to retrieve max row_num along with the corresponding customer details.
    select * from (SELECT ROWNUM ROWNUM_1, CUSTOMER_NO, CUSTOMER_NAME, CUSTOMER_DOJ, CUSTOMER_MOBILENO FROM CUSTOMER order by rownum_1 desc) where rownum<=1 ;

  • Nested Group Function without Group By Problem

    Hey everyone,
    I have 3 tables as below:
    TABLES
    ITEM (Item_no, Item_price, desc)
    DeliveryItem (delivery_no, item_no, quantity)
    Delivery (delivery_no, delivery_date)
    SELECT desc, MAX(SUM(quantity)) FROM DeliveryItem, Item, Delivery WHERE Item.item_no = DeliveryItem.item_no AND Delivery.delivery_no = deliveryitem.delivery_no;
    And I'm trying to output description of most delivered item but I got an error like SQL Error: ORA-00978: nested group function without GROUP BY. Could you help me to fix my code?
    Thanx

    Hi,
    DESC is not a good column name; you could get errors if the parser thinks it means DESCending. I used DESCRIPTION instead, below.
    I think the best way is to do the SUM in a sub-query, lkike this:
    WITH     got_r_num     AS
         SELECT       item_no
         ,       SUM (quantity)     AS total_quantity
         ,       RANK () OVER (ORDER BY  SUM (quantity) DESC)     AS r_num
         FROM       deliveryitem
         GROUP BY  item_no
    SELECT     i.description
    ,     r.total_quantity
    FROM     got_r_num     r
    JOIN     item          i     ON     r.item_no     = i.item_no
    WHERE     r.r_num     = 1
    ;If you want to do it without a sub-query:
    SELECT       MIN (i.description) KEEP (DENSE_RANK LAST ORDER BY SUM (di.quantity)
                        AS description
    ,       MAX (SUM (quantity))     AS total_quantity
    FROM       deliveryitem     di
    JOIN       item          i     ON     d1.item_no     = i.tiem_no
    GROUP BY  i.description
    ;If you do nested aggegate functions, then every column in the SELECT clause must be an aggregate applied to either
    (a) another aggregate, or
    (b) one of the GROUP BY expressions.
    That's why you got the ORA-00937 error.
    This second approach will only display one row of output, so If there is a tie for the item with the greatest total_quantity, only one description will be shown. The RANK method will show all items that had the highest total_quantity.
    It looks like the delivery table plays no role in this problem, but it there's some reason for including it, you can join it tpo either query above.
    Of course, unless you post test copies of your tables (CREATE TABLE and INSERT statements) I cn't test anything.
    Edited by: Frank Kulash on Nov 6, 2010 10:57 AM

  • My coding execution is slow due to Group function

    Hai All
    I have genetating an attendace function by day today process
    My coding is
    declare
    bar_code varchar2(25);
    in_time varchar2(25);
    out_time varchar2(25);
    Cursor c1 is
    Select BARCODE,BARDATE,BARTIME
    From temp_attendance
    group by barcode,bardate,bartime
    ORDER BY BARCODE,bartime;
    begin
    for r1 in c1 loop
    declare
    bar_code varchar2(25);
    begin
    select barcode into bar_code from dail_att where barcode=r1.barcode and attend_date=r1.bardate;
    For r in (select empcode,empname,barcode,intime,intrin,introut,addin,addout,outtime,attend_date from dail_att)loop
    if r1.bartime between 1630 and 1830 and r.intime between 0715 and 0915 then
    -- update dail_att set outtime=(select max(bartime) from temp_attendance group by barcode,bardate
    -- having temp_attendance.barcode=dail_att.barcode and temp_attendance.bardate= dail_att.attend_date );
    update dail_att set outtime=r1.bartime where barcode=r1.barcode and attend_date=r1.bardate and intime is not null;
    elsif r1.bartime between 1930 and 2130 and r.intime between 1145 and 1245 then
         update dail_att set outtime=r1.bartime where barcode=r1.barcode and attend_date=r1.bardate and intime is not null;
    --update dail_att set outtime=(select max(bartime) from temp_attendance  group by barcode,bardate
              -- having temp_attendance.barcode=dail_att.barcode and temp_attendance.bardate= dail_att.attend_date );
    elsif r.intrin is null and r.intime is not null then
         update dail_att set intrin=r1.bartime where barcode=r1.barcode and attend_date=r1.bardate and
         intime is not null and introut is null and addin is null and addout is null;
    elsif r.introut is null and r.intime is not null and r.intrin is not null then
         update dail_att set introut=r1.bartime where barcode=r1.barcode and attend_date=r1.bardate and intime is not null and intrin is not null and addin is null and addout is null;
         end if;     
    end loop;
         exception
         when no_data_found then
         if r1.bartime between 0715 and 0915 or r1.bartime between 1145 and 1245 or r1.bartime between 1700 and 1800 then
              insert into dail_att(barcode,intime,attend_date)
              (select r1.barcode,min(r1.bartime),r1.bardate from temp_attendance group by r1.barcode,r1.bardate);
         elsif r1.bartime between 0100 and 0630 then
              update dail_att set outtime=r1.bartime where barcode=r1.barcode and attend_date=r1.bardate-1;
         end if;
         end;
    end loop;
    commit;
    end;
    when i to generate 700 records without Max function i working and takes time.But when i use MAX Function its not working and taking a lot of time why
    Pls tell me some solution to rectify these problem
    While using Max function with sample data it works fine..
    Thanks & Regards
    Srikkanth.M

    Hello Sir
    Thanks for your reply
    As of now i have completed first part and in that little bit problems..
    First step Fetch the records as text file and stores into table T1
    and the next step is i have seperated the text using substring and stores in different columns of a table
    There are two shifts 0815 to 1645 and 1200 and 2000
    Here I rep IN and O rep OUT
    Empno date time inout
    001 01-01-10 0815 I
    002 01-01-10 0815 I
    003 01-01-10 0818 I
    001 01-01-10 1100 0
    001 01-01-10 1130 I
    002 01-01-10 1145 0
    002 01-01-10 1215 I
    004 01-01-10 1200 I
    005 01-01-10 1215 I
    004 01-01-10 1315 O
    004 01-01-10 1345 I
    001 01-01-10 1645 0
    002 01-01-10 1715 0
    003 01-01-10 1718 0
    004 01-01-10 2010 0
    005 01-01-10 2015 0
    This is my T1 table i have taken data from text file and stored in this table from this table i need to move data to another table T2
    T2 contains like this
    Empno Intime Intrin Introut Outtime Date
    001 0815 1100 1130 1645 01-01-10
    002 0815 1145 1215 1715 01-01-10
    003 0818 1718 01-01-10
    004 1200 1315 1345 2010 01-01-10
    005 1215 2015 01-01-10
    This what i am trying to do man but i have little bit problems Pls give some solution with good example
    Thanks & Regards
    Srikkanth.M

  • Sql group function

    I am trying to calculate the average cost of the books for each customer. The output should contain the customer#, name and the average order.
    This is the code I have:
    SELECT AVG(SUM((retail-cost)*quantity))
    FROM orders JOIN orderitems USING (order#)
    JOIN books USING (isbn)
    JOIN customers USING (customer#)
    GROUP BY order#, customer#;
    I get either, over 8000 for the average order or about 33 for the average order when I don't include the name, customer number or anything else in the code. If I add any other information, I get:
    SELECT customer#, AVG(SUM((retail-cost)*quantity))
    ERROR at line 1:
    ORA-00937: not a single-group group function
    I'm sure there is something small that I am missing, but cannot see it.

    Hi,
    Whenever you have a question, you should post a little sample data and the results you want to get from that data.
    I don't know where you're coming from, or where you want ot go, so it's hard to give you good directions. All I have is your current route plan, which must be incorrect, or else you wouldn't be asking anything.
    I see you are nesting aggregate functions. That's very rare in real life, and the result is always to produce one row of output, regardless of what's in the GROUP BY clause.
    I suspect you want to do a SUM in a sub-query, and then do AVG on the results in the main query. Without seeing your sample data and desired results, I'm just guessing.

  • How  to exclude columns dynamic using group functions or any other function

    i have a select that look like this
    SELECT *
      FROM (SELECT DISTINCT gruposcomerciales.cod_gcomercial,
                            gruposcomerciales.descripcion,
                            DECODE (:0,
                                    'OFICIAL', pbeoficialesjerarquia.oficial_id,
                                    NULL
                                   ) oficial_id,
                            DECODE (:0,
                                    'BANCA', NULL,
                                    pbeoficialesjerarquia.id_cluster
                                   ) id_cluster,
                            pbeoficialesjerarquia.cod_banca
                       FROM grupos_comerciales gruposcomerciales,
                            clientes clientes,
                            clientes_oficiales clientesoficiales,
                            pbe_oficiales_jerarquia pbeoficialesjerarquia,
                            bancos_clientes bancosclientes
                      WHERE gruposcomerciales.cod_gcomercial =  bancosclientes.cod_gcomercial
                        AND clientes.ID = bancosclientes.cliente_id
                        AND clientes.ID = clientesoficiales.cliente_id
                        AND bancosclientes.cliente_id = clientesoficiales.cliente_id
                        AND bancosclientes.entidad_id = clientesoficiales.entidad_id
                        AND clientesoficiales.oficial_id = pbeoficialesjerarquia.oficial_id
                        AND gruposcomerciales.cod_gcomercial <> 'SINGRUPO100'
                        AND clientesoficiales.tipo_oficial = 'PRINCIPA'
                        AND bancosclientes.estado_clientes_ibs = 'ACTIVO') qrslt
    WHERE (cod_banca = :1 AND id_cluster = :2 AND oficial_id = :3)This SQL duplicate rows so i use a distinct to exclude duplicate rows and decode to set null for columns that not are interested at runtime to avoid duplicate rows
    I use decode to dynamicly set null (a way fo not show duplicate rows from colums)
    If :0 is OFICIAL its show 3 columns values for oficial_id,cod_banca,id_cluster
    if :0 is CLUSTER its show 2 columns values for cod_banca,id_cluster
    There is a way to group by columns without take in consideration duplicate rows of other columns

    If i do my query passing pameters to [b]OFICIAL_ID, ID_CLUSTER, AND COD_BANCA get this info
    COD_GCOMERCIAL  DESCRIPCION                          OFICIAL_ID ID_CLUSTER COD_BANCA
    ==============  ===========                          ========== ========== =========
    GRUPOALC1149     GRUPO ALCAP                          JESS329     39        BANCAEMP
    GRUPOALQ533     GRUPO ALQUILERES NACIONALES          JESS329     39        BANCAEMP
    GRUPOANA649     GRUPO ANA MARIA CHEN                 JESS329     39        BANCAEMP
    GRUPOANC253     GRUPO ANCON                          JESS329     39        BANCAEMP
    GRUPOARI104     GRUPO ARIAS Y DE LA GUARDIA          JESS329     39        BANCAEMP
    GRUPOATL813     GRUPO ATLAS CORPORATION              JESS329     39        BANCAEMP
    IF I pass parameters to ID_CLUSTER, AND COD_BANCA i get
    COD_GCOMERCIAL  DESCRIPCION                          OFICIAL_ID ID_CLUSTER COD_BANCA
    ==============  ===========                          ========== ========== =========
    GRUPOALC1149     GRUPO ALCAP                                    39        BANCAEMP
    GRUPOALQ533     GRUPO ALQUILERES NACIONALES                    39        BANCAEMP
    GRUPOANA649     GRUPO ANA MARIA CHEN                           39        BANCAEMP
    GRUPOANC253     GRUPO ANCON                                    39        BANCAEMP
    GRUPOARI104     GRUPO ARIAS Y DE LA GUARDIA                    39        BANCAEMP
    GRUPOATL813     GRUPO ATLAS CORPORATION                        39        BANCAEMP
    IF I pass parameters to COD_BANCA i get
    COD_GCOMERCIAL  DESCRIPCION                          OFICIAL_ID ID_CLUSTER COD_BANCA
    ==============  ===========                          ========== ========== =========
    GRUPOALC1149     GRUPO ALCAP                                            BANCAEMP
    GRUPOALQ533     GRUPO ALQUILERES NACIONALES                            BANCAEMP
    GRUPOANA649     GRUPO ANA MARIA CHEN                                   BANCAEMP
    GRUPOANC253     GRUPO ANCON                                            BANCAEMP
    GRUPOARI104     GRUPO ARIAS Y DE LA GUARDIA                            BANCAEMP
    GRUPOATL813     GRUPO ATLAS CORPORATION                                BANCAEMPMy decode on select do this functionality of set null to columns because there are sometimes in wich oficial_id or id_cluster get rows
    that i dont need take in consideration.
    this get overhead on my select and on 10g using inline views by dblink not work fine
    this decode on my select do this functionality
    decode('OFICIAL', 'OFICIAL' , pbeoficialesjerarquia.oficial_id, NULL) oficial_id,
    decode('OFICIAL', 'BANCA' , NULL, pbeoficialesjerarquia.id_cluster) id_cluster,
    So i know oracle has a lot off grouping function some of this should help me to get this work
    Tnx Warren

  • Grouping functionality for Bid Invitations.

    Is there any way to group external requirements to Bid invitations.

    Hi,
    When you press the Submit to Grouping button in the Carry Out Sourcing option ,report BBP_SC_TRANSFER_GROUPED must be executed.
    Read the Documentation given in the report BBP_SC_TRANSFER_GROUPED .
    Short text
    Automatic Grouping of Requirements for POs and Bid Invitations
    Purpose
    This report groups together requirement items to which a valid source of supply has been assigned (for grouping into purchase orders) and whose product category has been enabled for grouping (sourcing option in Customizing).
    In cases where assignment is automatic, the system sets the requirement as Transferred to Grouping.
    In cases where a source of supply is assigned manually, the purchaser has to choose the function Transfer to Grouping in the sourcing application for the requirements.
    You should schedule this report periodically, and make sure that the report is run separately for each document type (purchase order, bid invitation).
    Prerequisites
    You have assigned each relevant product category to a sourcing option for grouping, for example, Automatic Grouping, Sourcing for Item Without Assigned Source of Supply.
    For more information, see the Implementation Guide (IMG) for Supplier Relationship Management: SRM Server -> Sourcing -> Define Sourcing for Product Categories.
    Features
    Report BBP_SC_TRANSFER_GROUPED provides the following functions:
    For automatically grouping requirements into purchase orders:
    It selects all requirements that have a unique valid source of supply, and have been set up for grouping. You can use selection criteria to further restrict the requirements that are selected.
    It groups requirements by vendor. In other words, it groups together all of the requirements that can be sent to the same vendor.
    You can use a Business Add-In (BAdI) to make additional groupings. If the standard system groups together certain requirements, but you want to create them in separate local purchase orders, use the BAdI BBP_GROUP_LOC_PO for this purpose.
    Note that there are some limitations to creating purchase orders this way. For example, it is not possible to group together requirements with different vendors in the same purchase order. This report uses the same differentiation criteria as the sourcing application (for example, business partner, company code, product type).
    It uses function module BBP_PD_PO_CHECK to check the assignment to a source of supply. The report passes on requirements to function module BBP_PD_SC_TRANS_MULTI_LOC_PO, where they are grouped, and converted into a purchase order.
    In a production run (that is, not a test run), the system issues error messages to the Application Monitor. Example: A contract item assigned during sourcing is no longer valid when grouping is about to occur. This error appears in the Application Monitor. The system moves the requirement item to the worklist of the sourcing application.
    The Application Monitor assigns errors to one of the following categories:
    Shopping Cart: Automatic Grouping
    Purchase Order: Automatic Grouping
    If necessary, you can copy this report, and change the selection criteria to suit your needs. You can start the report in test mode.
    Note: In the sourcing application, you can determine which requirements are planned for the next report run by choosing Find -> For Grouping.
    For automatically grouping requirements into bid invitations:
    It selects all requirements that have been set up for grouping. A source of supply is not required. You can use selection criteria to further restrict the requirements that are selected.
    You can use the BAdI BBP_TRANSFER_GROUP to take bid invitations that have been grouped by the system, and then split them up on the basis of your own customer-specific criteria. You can use the BAdI BBP_SAVE_BID_ON_HOLD to determine whether or not a bid invitation should be published automatically.
    The report passes on requirements to function module BBP_PD_SC_TRANSFER_MULTI_RFQ, where they are grouped, and converted into a bid invitation.
    In a production run (that is, not a test run), the system issues error messages to the Application Monitor.
    If necessary, you can copy this report, and change the selection criteria to suit your needs. You can start the report in test mode.
    Note: In the sourcing application, you can determine which requirements are planned for the next report run by choosing Find -> For Grouping.
    Output
    The report displays the purchase orders or bid invitations resulting from the grouping of the requirements. It also shows incorrect requirements that the system was unable to group into a purchase order or bid invitation.
    Related thread:
    SOCO Submit to Grouping functionality?
    Re: Bid invitation
    BR,
    Disha.
    <b>Pls reward points for useful answers.</b>

  • Error while using group function

    Oracle forms6i
    Hai
    While i am compile my coding it compile successfully, but when i tried to executes i shows error in group function
    my coding is
    if (cnt<>0 ) then
    select BARCODE,INTIME,OUTTIME into today_bar,today_in,today_out from dail_att where BARCODE= :Barcode
    and ATTEND_DATE = :bardate;
    update dail_att set outtime = max(:bartime) where barcode= :barcode
    and ATTEND_DATE = :bardate;
    else
    if (cnt2<>0 ) then
    select INTIME,OUTTIME into yest_in,yest_out from dail_att where BARCODE= :Barcode
    and ATTEND_DATE = :bardate-1;
    if(yest_in is not null and yest_out is null) then
    update dail_att set outtime =max(:bartime) where barcode= :barcode
    and ATTEND_DATE = :bardate-1;
    else
    insert into dail_att(barcode,intime,attend_date)
    values(:barcode,min(:bartime),:bardate);
    end if;
    else
    if :bartime between 0100 and 0630 then
    insert into dail_att(barcode,intime,attend_date)
    values(:barcode,min(:bartime),:bardate-1);
    update dail_att set outtime = max(:bartime) where barcode= :barcode
    and ATTEND_DATE = :bardate-1;
    else
    insert into dail_att(barcode,intime,attend_date)
    values(:barcode,:min(bartime),:bardate);
    end if;     
    end if;
    end if;
    while i am trying to this groupfunction it throws error while i use having tell me how to use group function and where
    to use
    Regadrs
    Srikkanth.M

    Hai sir
    I had a table that contain fields
    EMPCODE NUMBER
    EMPNAME VARCHAR2(25)
    BARCODE VARCHAR2(25)
    INTIME VARCHAR2(25)
    OUTTIME VARCHAR2(25)
    INTRTIMEIN VARCHAR2(25)
    INTROUTTIME VARCHAR2(25)
    PERTIMEIN VARCHAR2(25);
    PERTIMEOUT VARCHAR2(25);
    ATTEND_DATE DATE ;
    Consider that a table with 6 fields ie timein,intrtimein,pertimein,pertimeout,intrtimeout,timeout
    I have generating a attendance table and a table contain 6 various times for an employees and we need to arrange it in order
    0815,0816,1230,1250,1645,1646
    If 0815 is the starting time then timein ie mintime
    0816 stored to be in intrtime
    then1250 then it stored in pertimein
    then 1230 then it stored in pertimeout
    then 1645 stored in intrtimeout
    then 1646 stored in timeout
    I tried with max and min function but its not working properly pls tell me some solutions
    Thanks & Regards
    Srikkanth.M

  • How to use group function in insert or update

    Hai All
    How can we use group function in insert or update statement
    I am generating an attendance so i have different set of timing for example
    0800,1200,1230, 1700 and i need to insert into these data into table with min value to intime and max value to
    outtime and othere to inertval time in or out
    Pls tell me with some example
    For example
    For INSERT
    insert into T2 (barcode,empcode,intime,attend_date)
                   values(r2.cardn,r2.enpno,MIN(r2.ptime),r2.pdate);
    For UPDATE
    update dail_att set outtime= MAX(r2.ptime) where empcode=r2.enpno and barcode=r2.cardn and
    attend_date=r2.pdate;
    Here instead of where i need to use having so pls tell how to use
    Thanks & Regards
    Srikkanth.M

    Hai Man
    R2 is not a table name its a record
    Let me explain clearly
    I have to generate daily attendance for lot of employees So i have two table t1 and t2
    T1 consist of three column empno,date,time
    T2 consist of empno,name,date,intime,outtime,intrin,introut
    So now i need to give the T1 Min value Of time to T2 Intime and T1 Max value of Time to T2 Outtime fields so there so many records while i am using
    max(time) it gives the max value of all so i seperated by group function so now i have an error in subquery ie it is an single row subquery so i need to use multiple row subquery how i can use multiple row subquery in update statement
    Thanks In Advance
    Srikkanth.M

  • PL/SQL equivalent of T-SQL - "group function is not allowed here"

    Hi all, hope someone can give me a hand as I'm pretty stuck! I have been trying to convert some MS SQL Server T-SQL statements into Oracle PL/SQL and am stuck on the below one:
    SELECT
    CA.AssessmentID,
    (SELECT ProductName + ISNULL(' - ' + PrincipalBenefit,'')
    FROM rptPolicySnapshot WHERE PolicyID = MAX(CA.PolicyID)
    AND SnapshotID = 1),
    MAX(CA.PolicyID)
    FROM rptClaimInvoiceLineSnapshot CIL
    INNER JOIN rptClaimAssessmentSnapshot CA
    ON CIL.AssessmentID = CA.AssessmentID
    AND CIL.SnapshotID = CA.SnapshotID
    WHERE CIL.SnapshotID = 1
    GROUP BY CA.AssessmentID
    This works fine in MSSQL but returns the below error in Oracle:
    'ORA-00934: group function is not allowed here'
    If I take out the subquery the query works fine.
    Any ideas as to the syntax? I am new to Oracle so not sure as to how I should go about writing this.
    Thanks in advance!
    Leo

    WITH x AS (SELECT   ca.assessmentid,
                        MAX (ca.policyid) policy_id
               FROM rptclaiminvoicelinesnapshot cil
                    INNER JOIN rptclaimassessmentsnapshot ca
                        ON cil.assessmentid = ca.assessmentid
                       AND cil.snapshotid = ca.snapshotid
               WHERE cil.snapshotid = 1
               GROUP BY ca.assessmentid
    SELECT x.assessment_id,
           x.policy_id,
           productname + decode(principalbenefit,null,null,' - ' || principalbenefit ) prodname
    FROM   rptpolicysnapshot, x
    WHERE  policyid = x.policy_id
    AND    snapshotid = 1I think that's in the neighbourhood.

  • 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

Maybe you are looking for