ORA-00979: not a GROUP BY expression

in Oracle Database; Please let me know what is the problem in following code, Code : SELECT rc.CUSTOMER_ID, rc.CUSTOMER_NAME, rc.STATUS, rcta.TRX_DATE, rcta.TRX_NUMBER, rctta.NAME, ...

+ Post Reply + Post New Topic
Results 1 to 6 of 6
  1. #1
    RIAZ's Avatar
    RIAZ is offline Forum Advisor RIAZ is on a distinguished road
    Join Date
    01 Apr 2009
    Posts
    53
    Document Uploads
    0

    Helpful? Yes No

    ORA-00979: not a GROUP BY expression

    Please let me know what is the problem in following code,

    Code :
    SELECT rc.CUSTOMER_ID, rc.CUSTOMER_NAME, rc.STATUS,
    rcta.TRX_DATE, rcta.TRX_NUMBER,
    rctta.NAME,
    sum(rctla.EXTENDED_AMOUNT) DR
    FROM RA_CUSTOMERS rc,
    RA_CUSTOMER_TRX_ALL rcta,
    RA_CUST_TRX_TYPES_ALL rctta,
    RA_CUSTOMER_TRX_LINES_ALL rctla
    WHERE rc.CUSTOMER_NAME = 'ABC'
    AND rc.CUSTOMER_ID = rcta.BILL_TO_CUSTOMER_ID
    AND rcta.COMPLETE_FLAG = 'Y'
    AND rctta.CUST_TRX_TYPE_ID = rcta.CUST_TRX_TYPE_ID
    AND rctla.CUSTOMER_TRX_ID = rcta.CUSTOMER_TRX_ID
    group by rcta.TRX_NUMBER

  2.    Club-Oracle Complementary E-Books and Magazines
    Get your free Complementary Copy of Oracle Magazine

    You can also browse the Free Magazines and E-Books section to see the complete list of free magazines, e-books and Whitepapers.

  3. #2
    tyro's Avatar
    tyro is offline Forum Genius tyro is on a distinguished road
    Join Date
    20 Aug 2008
    Location
    India
    Posts
    362
    Document Uploads
    0

    Helpful? Yes No

    Re: ORA-00979: not a GROUP BY expersion

    hi Riaz, you have to use all the columns you are using in your select statement within your group by clause

    Try this
    Code sql:
    SELECT   rc.customer_id, rc.customer_name, rc.STATUS, rcta.trx_date,
             rcta.trx_number, rctta.NAME, SUM (rctla.extended_amount) dr
        FROM ra_customers rc,
             ra_customer_trx_all rcta,
             ra_cust_trx_types_all rctta,
             ra_customer_trx_lines_all rctla
       WHERE rc.customer_name = 'ABC'
         AND rc.customer_id = rcta.bill_to_customer_id
         AND rcta.complete_flag = 'Y'
         AND rctta.cust_trx_type_id = rcta.cust_trx_type_id
         AND rctla.customer_trx_id = rcta.customer_trx_id
    GROUP BY rc.customer_id,
             rc.customer_name,
             rc.STATUS,
             rcta.trx_date,
             rcta.trx_number,
             rctta.NAME

  4. #3
    RIAZ's Avatar
    RIAZ is offline Forum Advisor RIAZ is on a distinguished road
    Join Date
    01 Apr 2009
    Posts
    53
    Document Uploads
    0

    Helpful? Yes No

    Re: ORA-00979: not a GROUP BY expression

    It works, thanks. Well i could not understand the logic behind this, would you please explain why we need to add all the select cols into group by clause?

    In my understanding we should only write the col in group by clause that is required for grouping. Is not it ? This logic works well with a single table.

  5. #4
    tyro's Avatar
    tyro is offline Forum Genius tyro is on a distinguished road
    Join Date
    20 Aug 2008
    Location
    India
    Posts
    362
    Document Uploads
    0

    Helpful? Yes No

    Re: ORA-00979: not a GROUP BY expression

    you are using the aggregate function SUM. When you use a group by clause it divides your resultant rows into sets and then the aggregate function is used on all the sets. So when you selecting multiple columns in your select statement, the grouping also has to be based on all those columns.

  6. #5
    RIAZ's Avatar
    RIAZ is offline Forum Advisor RIAZ is on a distinguished road
    Join Date
    01 Apr 2009
    Posts
    53
    Document Uploads
    0

    Helpful? Yes No

    Re: ORA-00979: not a GROUP BY expression

    thank you.

  7. #6
    rajavu's Avatar
    rajavu is offline Forum Genius rajavu is on a distinguished road
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    482
    Document Uploads
    0

    Helpful? Yes No

    Re: ORA-00979: not a GROUP BY expression

    Another way of doing it without Group by but with Partition by
    This is Analytic function .

    Code sql:
    SQL> SELECT DEPTNO,JOB, SUM(SAL)
      2 FROM EMP
      3 GROUP BY DEPTNO,JOB;
     
        DEPTNO JOB         SUM(SAL)
    ---------- --------- ----------
            20 CLERK           1900
            30 SALESMAN        5600
            20 MANAGER         2975
            30 CLERK            950
            10 PRESIDENT       5000
            30 MANAGER         2850
            10 CLERK           1300
            10 MANAGER         2450
            20 ANALYST         6000
     
    9 rows selected.
     
    SQL> SELECT DISTINCT  DEPTNO,JOB ,
      2         SUM(SAL)  OVER  (PARTITION BY DEPTNO,JOB )
      3 FROM EMP   ;
     
        DEPTNO JOB       SUM(SAL)OVER(PARTITIONBYDEPTNO,JOB)
    ---------- --------- -----------------------------------
            10 CLERK                                    1300
            30 SALESMAN                                 5600
            20 MANAGER                                  2975
            30 MANAGER                                  2850
            10 PRESIDENT                                5000
            20 CLERK                                    1900
            20 ANALYST                                  6000
            30 CLERK                                     950
            10 MANAGER                                  2450
     
    9 rows selected.
     
    SQL>

    Raj.

Similar Threads

  1. ORA-00979: not a GROUP BY expression
    By Patience in forum SQL PL/SQL
    Replies: 2
    Last Post: 12-04-2009, 05:00 PM
  2. Problem of GROUP BY
    By RIAZ in forum SQL PL/SQL
    Replies: 3
    Last Post: 06-18-2009, 04:31 AM
  3. Oracle SQL CASE Expression
    By ygsunilkumar in forum SQL PL/SQL
    Replies: 4
    Last Post: 05-12-2009, 06:16 AM
  4. SQL Expression
    By ygsunilkumar in forum SQL PL/SQL
    Replies: 1
    Last Post: 05-05-2009, 04:15 AM