This post shows 2 pieces of SQL. The first has a group by, which I have already shown in other posts. The second restricts the output to show groups of 50 or more. You are not allowed to use a where clause to filter the output from a group by statement, you must use having instead, as shown in the example:
SQL> select object_type, count(*)
2 from dba_objects
3 group by object_type
4 /
OBJECT_TYPE COUNT(*)
------------------ ----------
CLUSTER 10
CONSUMER GROUP 4
DATABASE LINK 12
DIRECTORY 1
EVALUATION CONTEXT 1
FUNCTION 129
INDEX 992
INDEX PARTITION 25
LIBRARY 62
LOB 52
MATERIALIZED VIEW 2
OPERATOR 2
PACKAGE 321
PACKAGE BODY 313
PROCEDURE 183
QUEUE 8
RESOURCE PLAN 3
SEQUENCE 168
SYNONYM 1782
TABLE 830
TABLE PARTITION 27
TRIGGER 90
TYPE 480
TYPE BODY 21
VIEW 2414
25 rows selected.
SQL> select object_type, count(*)
2 from dba_objects
3 group by object_type
4 having count(*) > 50
5 /
OBJECT_TYPE COUNT(*)
------------------ ----------
FUNCTION 129
INDEX 992
LIBRARY 62
LOB 52
PACKAGE 321
PACKAGE BODY 313
PROCEDURE 183
SEQUENCE 168
SYNONYM 1782
TABLE 830
TRIGGER 90
TYPE 480
VIEW 2414
13 rows selected.
SQL>
2 from dba_objects
3 group by object_type
4 /
OBJECT_TYPE COUNT(*)
------------------ ----------
CLUSTER 10
CONSUMER GROUP 4
DATABASE LINK 12
DIRECTORY 1
EVALUATION CONTEXT 1
FUNCTION 129
INDEX 992
INDEX PARTITION 25
LIBRARY 62
LOB 52
MATERIALIZED VIEW 2
OPERATOR 2
PACKAGE 321
PACKAGE BODY 313
PROCEDURE 183
QUEUE 8
RESOURCE PLAN 3
SEQUENCE 168
SYNONYM 1782
TABLE 830
TABLE PARTITION 27
TRIGGER 90
TYPE 480
TYPE BODY 21
VIEW 2414
25 rows selected.
SQL> select object_type, count(*)
2 from dba_objects
3 group by object_type
4 having count(*) > 50
5 /
OBJECT_TYPE COUNT(*)
------------------ ----------
FUNCTION 129
INDEX 992
LIBRARY 62
LOB 52
PACKAGE 321
PACKAGE BODY 313
PROCEDURE 183
SEQUENCE 168
SYNONYM 1782
TABLE 830
TRIGGER 90
TYPE 480
VIEW 2414
13 rows selected.
SQL>
No comments:
Post a Comment