DBAsupport.com Forums - Powered by vBulletin
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Question about SUM and GROUP BY

  1. #1
    Join Date
    Apr 2001
    Posts
    127

    Question about SUM and GROUP BY

    Hi,

    I have a generic question about SUM and GROUP BY:

    Suppose I have a table called test1. Now I have to return the following information:

    test1.col1, test1.col2, sum(test.col3) group by col2 only

    I write the following query but it doesn't work and returns the "ORA-00979: not a GROUP BY expression" error, any idea how to make it work?

    select test1.col1, test1.col2, sum(test.col3) from test1
    where test1.col2 not null
    group by test1.col2

    Thanks

  2. #2
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    When you do a GROUP BY all of the columns bust either be a part of an aggregate function or a part of the GROUP BY clause.

    Aggregate functions include, MIN(), MAX(), SUM(), AVG, STDDEV, etc.

  3. #3
    Join Date
    Mar 2004
    Location
    DC,USA
    Posts
    650
    Quote Originally Posted by zxmgh
    Hi,

    I have a generic question about SUM and GROUP BY:

    Suppose I have a table called test1. Now I have to return the following information:

    test1.col1, test1.col2, sum(test.col3) group by col2 only

    I write the following query but it doesn't work and returns the "ORA-00979: not a GROUP BY expression" error, any idea how to make it work?

    select test1.col1, test1.col2, sum(test.col3) from test1
    where test1.col2 not null
    group by test1.col2

    Thanks
    execute this to avoid ora-00797:

    Code:
    select test1.col1, test1.col2, sum(test.col3) from test1
    where test1.col2 not null
    group by test1.col2,test1.col1;
    "What is past is PROLOGUE"

  4. #4
    Join Date
    Apr 2001
    Posts
    127
    dbasan,

    Your code "group by test1.col2,test1.col1" is not what I want, I only want to group by test1.col2. Please clarify.

    Thanks

  5. #5
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    You'd better post some sample data and the result that you want
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  6. #6
    Join Date
    Mar 2004
    Location
    DC,USA
    Posts
    650
    Quote Originally Posted by zxmgh
    dbasan,

    Your code "group by test1.col2,test1.col1" is not what I want, I only want to group by test1.col2. Please clarify.

    Thanks
    the limitations of group by clause was clearly given by gandolf989.

    as per the code you have given, i have already given you one solution, the next solution is:

    Code:
    select test1.col2, sum(test.col3) from test1
    where test1.col2 not null
    group by test1.col2;
    Still it is not what you expect, then

    I would suggest you better read the Oracle documents once again. Or

    Go thru' this link http://www.dbasupport.com/forums/showthread.php?t=44794
    "What is past is PROLOGUE"

  7. #7
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    You can also do this, but don't expect the results to be consistent.

    Code:
    SELECT test1.col1, FIRST(test1.col2) col2, SUM ( TEST.col3 )
      FROM test1
     WHERE test1.col2 IS NOT NULL
     GROUP BY test1.col2
     ORDER BY test1.col1;

  8. #8
    Join Date
    Apr 2001
    Posts
    127
    Right now my query looks like this:

    SELECT ch2.col1,
    ch2.col2,
    chq.col3_qty
    FROM (SELECT ch1.col2, SUM(ch1.col3) AS col3_qty FROM test1 ch1
    WHERE ch1.col2 IS NOT NULL
    GROUP BY ch1.col2) chq
    INNER JOIN test1 ch2 ON (chq.col2 = ch2.col2)

    It did returns what I want, but is there a more efficient way to do this?

    Thanks

  9. #9
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Quote Originally Posted by zxmgh
    Right now my query looks like this:

    SELECT ch2.col1,
    ch2.col2,
    chq.col3_qty
    FROM (SELECT ch1.col2, SUM(ch1.col3) AS col3_qty FROM test1 ch1
    WHERE ch1.col2 IS NOT NULL
    GROUP BY ch1.col2) chq
    INNER JOIN test1 ch2 ON (chq.col2 = ch2.col2)

    It did returns what I want, but is there a more efficient way to do this?

    Thanks
    Today your query may return correct data b/c you have sample data.
    One day your query will fail.
    See below:
    Code:
    SQL> select * from state_city_sales ;
    
    ST CITY                   SALE_AMT
    -- -------------------- ----------
    MA CONCORD                     100
    NY CONCORD                     200
    NY NEWYORK                     400
    MA BOSTON                      300
    
    SQL> get w1
      1  select b.state , b.city, a.tot_amt
      2  from  ( select city, sum(sale_amt) tot_amt
      3            from state_city_sales group by city  ) a
      4* inner join state_city_sales b on (a.city = b.city )
    SQL> /
    
    ST CITY                    TOT_AMT
    -- -------------------- ----------
    MA BOSTON                      300   
    MA CONCORD                     300   ---> Wrong
    NY CONCORD                     300   ----> Wrong
    NY NEWYORK                     400
    We must know the table definition, PK , FK etc and sample data before rewriting your SQL statement.

    Tamil

  10. #10
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    21
    try using this = hope it helps....

    select a.state , a.city, sum(a.tot_amt)
    from state_city_sales a
    group by a.state , a.city


    Quote Originally Posted by tamilselvan
    Today your query may return correct data b/c you have sample data.
    One day your query will fail.
    See below:
    Code:
    SQL> select * from state_city_sales ;
    
    ST CITY                   SALE_AMT
    -- -------------------- ----------
    MA CONCORD                     100
    NY CONCORD                     200
    NY NEWYORK                     400
    MA BOSTON                      300
    
    SQL> get w1
      1  select b.state , b.city, a.tot_amt
      2  from  ( select city, sum(sale_amt) tot_amt
      3            from state_city_sales group by city  ) a
      4* inner join state_city_sales b on (a.city = b.city )
    SQL> /
    
    ST CITY                    TOT_AMT
    -- -------------------- ----------
    MA BOSTON                      300   
    MA CONCORD                     300   ---> Wrong
    NY CONCORD                     300   ----> Wrong
    NY NEWYORK                     400
    We must know the table definition, PK , FK etc and sample data before rewriting your SQL statement.

    Tamil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width