group by cube : Cube « Analytical Functions « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Analytical Functions » Cube 
group by cube
 



SQL> create table student (
  2  student_id  number primary key,
  3  student_name  varchar2(25),
  4  student_year  varchar2(15),
  5  student_major  varchar2(25) );

Table created.

SQL>
SQL> create table class (
  2  class_id    number primary key,
  3  class_desc  varchar2(35),
  4  credit_hrs  number(2) );

Table created.

SQL>
SQL> create table grades (
  2  student_id  number,
  3  class_id    number,
  4  assignment_desc varchar2(200),
  5  grade_received  number(3) );

Table created.

SQL>
SQL> insert into student values (1,'Tom','First','Art');

row created.

SQL> insert into student values (2,'Jack','First','Med');

row created.

SQL> insert into student values (3,'Peter','First','History');

row created.

SQL> insert into student values (4,'Jason','First','Science');

row created.

SQL> insert into student values (5,'Joe','Second','Education');

row created.

SQL> insert into student values (6,'Cat','Second','Finance');

row created.

SQL> insert into student values (7,'Sill','Second','Art');

row created.

SQL> insert into student values (8,'Bill','Second','Med');

row created.

SQL> insert into student values (9,'Mary','Third','History');

row created.

SQL>
SQL> insert into class values (1,'Public Speaking 101',3);

row created.

SQL> insert into class values (2,'English 101',3);

row created.

SQL> insert into class values (3,'English 201',3);

row created.

SQL> insert into class values (4,'English 301',3);

row created.

SQL> insert into class values (5,'English 401',3);

row created.

SQL> insert into class values (6,'Marketing 101',3);

row created.

SQL> insert into class values (7,'Child Development 101',3);

row created.

SQL> insert into class values (8,'Golf for Novices',2);

row created.

SQL> insert into class values (9,'Biology 101',4);

row created.

SQL>
SQL> insert into grades values (1,1,'Exam 1',94);

row created.

SQL> insert into grades values (7,1,'Exam 1',88);

row created.

SQL> insert into grades values (4,1,'Exam 1',85);

row created.

SQL> insert into grades values (1,1,'Exam 2',87);

row created.

SQL> insert into grades values (7,1,'Exam 2',89);

row created.

SQL> insert into grades values (4,1,'Exam 2',91);

row created.

SQL> insert into grades values (1,1,'Paper 1',90);

row created.

SQL> insert into grades values (7,1,'Paper 1',82);

row created.

SQL>
SQL>  select c.class_desc as class, s.student_name as student, avg(g.grade_receivedas grade_avg,
  2          grouping(c.class_descas class_ind, grouping(s.student_nameas stud_ind
  3     from class c, student s, grades g
  4    where c.class_id = g.class_id
  5      and s.student_id = g.student_id
  6    group by cube (c.class_desc, s.student_name;

CLASS                     STUDENT                        GRADE_AVG  CLASS_IND   STUD_IND
------------------------- ------------------------------ --------- ---------- ----------
                                                             88.25          1          1
                          Tom                                90.33          1          0
                          Sill                               86.33          1          0
                          Jason                              88.00          1          0
Public Speaking 101                                          88.25          0          1
Public Speaking 101       Tom                                90.33          0          0
Public Speaking 101       Sill                               86.33          0          0
Public Speaking 101       Jason                              88.00          0          0

rows selected.

SQL>
SQL>
SQL> drop table class;

Table dropped.

SQL> drop table student;

Table dropped.

SQL> drop table grades;

Table dropped.

   
  
Related examples in the same category
1. Cube implies reducing tables by rolling up different columns (dimensions)
2. Grouping with Cube
3. CUBE clause: return rows containing a subtotal for all combinations of columns included in the CUBE clause along with a total
4. The sequence of the columns passed into Cube function
5. Use CUBE and RANK() to get all rankings of sales by product type ID and employee ID
6. Demonstrate Partial CUBE operation
7. use GROUPING SETS to produce just a subset of CUBE
8. Count employees, group by CUBE(department no, job title)
9. group by CUBE(department no, job title)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.