Home > sql > not a group by expression?

not a group by expression?

April 1st, 2009

How do I count all objects in the current schema?


SQL*Plus: Release 10.2.0.1.0 - Production on Wed Apr 1 09:58:46 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle 12g Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Expensive option

SQL> select
  2    user,
  3    count(*)
  4  from
  5    user_objects
  6  where
  7    status='INVALID';
  user,
  *
ERROR at line 2:
ORA-00937: not a single-group group function

Disconnected from Oracle 12g Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Expensive option

It does not seem to work … What’s wrong?

Bookmark and Share

  1. Leo Anderson
    April 1st, 2009 at 10:45 | #1

    Hi Laurent,

    Either you are very very tired, or I’ve missed something ?!
    select
    user,
    count(*)
    from
    user_objects
    where
    status=’INVALID’
    group by user

  2. Laura
    April 1st, 2009 at 11:05 | #2

    Disconnected from Oracle 12g Enterprise Edition Release 12.1.0.1.0 - 64bit Production With the Expensive option :)))))

    That could probably a reason ;)

  3. April 1st, 2009 at 11:05 | #3

    Hi Leo,

    Yes he missed that… but wouldn’t you expect Oracle 12g WITH expensive option to think for you?

  4. April 1st, 2009 at 11:07 | #4

    Laurent,

    I love the version banner, I take it that’s hacked and not a beta version your working with.

    The “Expensive Option” bit is particularly amusing.

    Thanks,
    Alan…

  5. April 1st, 2009 at 11:10 | #5

    Ah is this an April Fools Day prank?

  6. April 1st, 2009 at 11:43 | #6

    Hey Laurent,

    You forgot:
    alter session set automatic_group_clause = true;

    PS. I’ve been wanting that alter session command since Oracle 7.3!

    Gareth

  7. April 1st, 2009 at 13:18 | #7

    what’s wrong ?
    well, beside “Oracle 12g …”
    it is wrong that you would never get
    ORA-00937: not a single-group group function

    probably if you would ask for
    1 select
    2 object_type,
    3 count(*)
    4 from
    5 user_objects
    6 where
    7 status=’INVALID’

    but not if you asked for
    1 select
    2 user,
    3 count(*)
    4 from
    5 user_objects
    6 where
    7 status=’INVALID’

    happyu April’s fools day !

  8. April 1st, 2009 at 13:29 | #8

    Obviously you are stuck on the 12g beta release, it is a well known bug, you should apply the bug fix #20090401 or wait for the next patchset !

  9. April 1st, 2009 at 17:53 | #9

    happy april fool of course :-)

    The query is perfectly fine to count all invalid objects in the current schema. The error and banner were fake of course :mrgreen:

    Adding a GROUP BY clause will suppress the output when no invalid object exists…

    
    SQL> select
      2       user,
      3       count(*)
      4  from
      5       user_objects
      6  where
      7       status='INVALID'
      8  ;
    
    USER                             COUNT(*)
    ------------------------------ ----------
    SCOTT                                   0
    
    SQL> select
      2       user,
      3       count(*)
      4  from
      5       user_objects
      6  where
      7       status='INVALID'
      8  group by
      9       user;
    
    no rows selected
    

  10. Chen Shapira
    April 1st, 2009 at 19:19 | #10

    Hey Laurent, what the Sum of Ten + Five? Please put easier spam protection, took me 15 minutes to figure this out :)

    Your post made my day!

  11. Bob Carlton
    April 1st, 2009 at 19:45 | #11

    worked for me after I applied the patch.

    SQL*Plus: Release 10.2.0.1.0 - Production on Wed Apr 1 09:58:46 2009

    Copyright (c) 1982, 2005, Oracle. All rights reserved.

    Connected to:
    Oracle 12g Enterprise Edition Release 12.1.0.1.0 - 64bit Production
    With the Expensive option

    SQL> select
    2 user,
    3 count(*)
    4 from
    5 user_objects
    6 where
    7 status=’INVALID’;

    Query finished, retrieving results…
    USER COUNT(*)
    —————————— ————————————–

    0 row(s) retrieved

    Disconnected from Oracle 12g Enterprise Edition Release 12.1.0.2.0 - 64bit Production
    With the Expensive option

  12. April 1st, 2009 at 20:13 | #12

    Laurent Schneider :
    happy april fool of course
    The query is perfectly fine to count all invalid objects in the current schema. The error and banner were fake of course

    Adding a GROUP BY clause will suppress the output when no invalid object exists…

    em ?
    sorry, but I don’t understand it does not work for me:

    sql > select user,count(*) from dual group by user;

    USER COUNT(*)
    —————————— ———-
    SOKRATES 1

    sql > select count(*) from dual where dummy=’X';

    COUNT(*)
    ———-
    1

    sql > select user,count(*) from dual where dummy=’X’ group by user;

    USER COUNT(*)
    —————————— ———-
    SOKRATES 1

    sql > select user,count(*) from user_objects where status=’INVALID’;

    USER COUNT(*)
    —————————— ———-
    SOKRATES 2

    sql > select user,count(*) from user_objects where status=’INVALID’ group by user;

    USER COUNT(*)
    —————————— ———-
    SOKRATES 2

    something wrong with my database ?

  13. April 1st, 2009 at 22:45 | #13

    Sokrates, if you have count(*)=2, the “GROUP BY whateverfunction” will give you 2. But if you have 0 row, the “GROUP BY whateverfunction” will give you “no rows returned” instead of 0

  14. April 2nd, 2009 at 18:40 | #14

    I was shocked since I understood the USER is gathered from my session info :)

    nice one Laurent

  15. April 28th, 2009 at 02:18 | #15

    @Laurent Schneider

    Great example why SQL is sometimes so misleading, Chris Date would have loved this example.

    What do you want today?

    “1 row selected” or “no rows selected”.

    Great example as a basis for a “Nullology” / “SQL is bad” discussion…

  16. April 28th, 2009 at 08:22 | #16

    if you group by and have no group, then no rows

  1. No trackbacks yet.