oracle database 10g sql -...

20
Oracle Database 10g SQL 2005 6

Upload: vanthu

Post on 05-Apr-2018

307 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g

SQL

2005 6

Page 2: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 2

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Oracle Database 10g

SQL

............................................................................................................................ 3

.................................................................................................................... 3

................................................................................................ 4

................................................................................................ 5

...................................................................................... 6

GROUPING SETS ................................................................................ 6

NULL ................................................................... 7

ROLLUP............................................................................................................... 8

ROLLUP ................................................................................................ 8

ROLLUP ................................................................... 9

CUBE.................................................................................................................. 10

CUBE ................................................................................................... 10

CUBE ...................................................................... 10

GROUP BY ................................................ 11

................................................................................................................ 13

.................................................................................................. 13

............................................................................................ 14

............................................................................................ 14

GROUPING SET .......................................................... 15

........................................................................................ 16

...................................................................................... 16

GROUPING_ID .................................................................................. 17

GROUP_ID ......................................................................................... 18

........................................................... 20

.......................................................................................................................... 21

Page 3: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 3

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Oracle Database 10g

SQL

GROUP BY

SQL

Oracle SQL

SQL SELECT GROUP BY

CUBE ROLLUP GROUPING

SETS

Oracle

Oracle Database 10g GROUP BY

Oracle SQL Analytic SQL Features in

Page 4: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 4

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Oracle Database 10g Oracle Database 10g SQL

The SQL Model Clause of Oracle Database 10g Oracle Database 10g

SQL Oracle OLAP Option

OLAP Option to Oracle

Database 10g Oracle Database 10g OLAP

Oracle Technology Network

SQL

Oracle SQL GROUP BY

SQL

Oracle Database 10g

GROUP BY 1

GROUP BY

1

GROUP BY

• GROUPING SETS

• CUBE ROLLUP

ANSI SQL-99

GROUP BY

Page 5: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 5

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

GROUPING SETS ROLLUP CUBE

OLAP

GROUP BY

Time

Department Region 3 Time

2003 2004 Department Video Sales Video Rentals

Region East West Central

2004

2004

Region Department Department Totals

Video Rental Profit Video Sales Profit

Central 82,000 85,000 167,000

East 101,000 137,000 238,000

West 96,000 97,000 193,000

Regional Totals 279,000 319,000 598,000

6

Region Department SUM GROUP BY

5 1

Video Rental Profit 279,000 East

238,000 12 6

Region Department GROUP BY 1

GROUP BY

GROUP BY 4 GROUP BY Region

Department GROUP BY Region GROUP BY Department

GROUP BY

Page 6: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 6

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

1 SELECT

GROUP BY

1

UNION ALL

UNION ALL

GROUPING SETS

GROUP BY GROUPING SETS

GROUPING SETS

SQL 3 Time, Region, Department Time,

Department Region, Department

SELECT Time, Region, Department, sum(Profit)

FROM Sales

GROUP BY GROUPING SETS ((Time, Region, Department),

(Time,Department), (Region,Department));

Time Region Department Profit

2003 Central VideoRental 75,000

2003 Central VideoSales 74,000

2003 East VideoRental 89,000

2003 East VideoSales 115,000

2003 West VideoRental 87,000

2003 West VideoSales 86,000

2003 [NULL] VideoRental 251,000

2003 [NULL] VideoSales 275,000

2004 Central VideoRental 82,000

2004 Central VideoSales 85,000

2004 East VideoRental 101,000

2004 East VideoSales 137,000

2004 West VideoRental 96,000

2004 West VideoSales 97,000

2004 [NULL] VideoRental 279,000

2004 [NULL] VideoSales 319,000

[NULL] Central VideoRental 157,000

[NULL] Central VideoSales 159,000

Page 7: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 7

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Time Region Department Profit

[NULL] East VideoRental 190,000

[NULL] East VideoSales 252,000

[NULL] West VideoRental 183,000

[NULL] West VideoSales 183,000

UNION ALL

SELECT Time, Region, Department, sum(Profit)

FROM salesTable

GROUP BY year, region, product

UNION ALL

SELECT Time, NULL, Department, sum(Profit)

FROM salesTable

GROUP BY Time, Department

UNION ALL

SELECT NULL, Region, Department, sum(Profit)

FROM salesTable

GROUP BY Region, Product;

SQL 3

Oracle 1

NULL

GROUP BY NULL

NULL

GROUPING SETS NULL Region

7 NULL 2003 Video Rental

NULL NULL

GROUPING

NULL

Oracle

Page 8: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 8

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

ROLLUP

ROLLUP

ROLLUP

ROLLUP

ROLLUP

SELECT . . . GROUP BY

ROLLUP( <grouping column reference list> )

ROLLUP

GROUP BY 1

ROLLUP n +1

ROLLUP Time Region Department

n=3 4

ROLLUP GROUPING SET ROLLUP

(a, b, c)

GROUPING SETS ( (a, b, c), (a, b), (a), ( ) ) ( )

ROLLUP

ROLLUP

SELECT Time, Region, Department,

sum(Profit) AS Profit FROM sales

GROUP BY ROLLUP(Time, Region, Dept)

• Time Region Dept : ROLLUP

GROUP BY

• Time Region : Time Region

Department

• Time : Time Region

Department 2

• :

Page 9: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 9

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Time Region Department Profit

2003 Central VideoRental 75,000

2003 Central VideoSales 74,000

2003 Central [NULL] 149,000

2003 East VideoRental 89,000

2003 East VideoSales 115,000

2003 East [NULL] 204,000

2003 West VideoRental 87,000

2003 West VideoSales 86,000

2003 West [NULL] 173,000

2003 [NULL] [NULL] 526,000

2004 Central VideoRental 82,000

2004 Central VideoSales 85,000

2004 Central [NULL] 167,000

2004 East VideoRental 101,000

2004 East VideoSales 137,000

2004 East [NULL] 238,000

2004 West VideoRental 96,000

2004 West VideoSales 97,000

2004 West [NULL] 193,000

2004 [NULL] [NULL] 598,000

[NULL] [NULL] [NULL] 1,124,000

ROLLUP

ROLLUP

• ROLLUP

ROLLUP

year, month, day ROLLUP country, state, city

• ROLLUP

ROLLUP

ROLLUP

1 ROLLUP

279,000 319,000 ROLLUP Time,

Region, Department ROLLUP

ROLLUP Time, Department, Region

CUBE

Page 10: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 10

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

CUBE

CUBE 1

CUBE

CUBE 1 SELECT

CUBE

SELECT . . . GROUP BY

CUBE ( <grouping column reference list> )

CUBE

CUBE

CUBE

Time, Region, Department ROLLUP

CUBE n

2n

ROLLUP CUBE GROUPING SET

CUBE(a, b, c)

GROUPING SETS ((a, b, c), (a, b), (a, c), (b, c), (a), (b),

(c) ())

CUBE

3 CUBE

8 2 3

SELECT Time, Region, Department,

SUM(Profit) AS Profit

FROM sales

GROUP BY CUBE (Time, Region, Dept);

CUBE ROLLUP 4 Time,

Region, Dept Time, Region Time

Region (Dept Time, Dept Region, Dept

CUBE

CUBE ROLLUP

CUBE CUBE 1

month, state, product

3

year, month, day

Page 11: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 11

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

17

CUBE 1

GROUP BY

ROLLUP CUBE

SELECT

ROLLUP

SELECT Year, Quarter, Month,

SUM(Revenues) AS Revenues

FROM Revenues

GROUP BY ROLLUP(Year, Quarter, Month);

Year Quarter Month Revenues

2004 Winter Jan 55,000

2004 Winter Feb 64,000

2004 Winter March 71,000

2004 Winter [NULL] 190,000

2004 Spring April 75,000

2004 Spring May 86,000

2004 Spring June 88,000

2004 Spring [NULL] 249,000

2004 Summer July 91,000

2004 Summer August 87,000

2004 Summer September 101,000

2004 Summer [NULL] 279,000

2004 Fall October 109,000

2004 Fall November 114,000

2004 Fall December 133,000

2004 Fall [NULL] 356,000

2004 [NULL] [NULL] 1,074,000

Page 12: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 12

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

1 ROLLUP

ROLLUP

(quarter, month)

GROUP BY ROLLUP (year, (quarter, month), day)

(quarter, month) SQL ROLLUP

Quarter Month year quarter

(year, quarter, month, day),

(year, quarter, month),

(year)

()

month year

SELECT Year, Quarter, Month,

SUM(Revenues) AS Revenues FROM Revenues

GROUP BY ROLLUP(Year, (Quarter, Month))

Year Quarter Month Revenues

2004 Winter Jan 55,000

2004 Winter Feb 64,000

2004 Winter March 71,000

2004 Spring April 75,000

2004 Spring May 86,000

2004 Sprint June 88,000

2004 Summer July 91,000

2004 Summer August 87,000

2004 Summer September 101,000

2004 Fall October 109,000

2004 Fall November 114,000

2004 Fall December 133,000

2004 [NULL] [NULL] 1,074,000

Page 13: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 13

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

ROLLUP CUBE

ROLLUP CUBE GROUPING SET

ROLLUP CUBE

GROUPING SET

GROUP BY GROUPING SETS (a, b), GROUPING SETS (c, d)

GROUPING SET a b 2 GROUPING

SET c d

(a, c) (a, d) (b, c) (b, d)

SQL

1

CUBE

ROLLUP ROLLUP

3

3

Page 14: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 14

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Time year, quarter, month

Product category, brand, item

Geography region, state, city

3

ROLLUP

ROLLUP

GROUPING SET

GROUP BY

ROLLUP

GROUP BY ROLLUP(year, quarter, month),

ROLLUP(category, brand, item),

ROLLUP(region, state, city)

GROUP BY ROLLUP

4

ROLLUP by Time ROLLUP by Product ROLLUP by Geography

year, quarter, month Category, brand, item region, state, city

year, quarter Category, brand region, state

year Category region

all times all products all geographies

SQL ROLLUP ROLLUP

64 4 4 4 3

ROLLUP 64

SQL

4 1

256

Page 15: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 15

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

GROUP BY 2

%

1

NULL NULL

GROUP BY NULL

NULL

Oracle GROUPING GROUPING_ID

GROUP_ID

GROUPING 1 GROUP BY

NULL 1 NULL

GROUPING 1 NULL

0 GROUPING()

SELECT

SELECT … [GROUPING(dimension column)…] …

GROUP BY … {CUBE | ROLLUP}

GROUPING

SELECT Time, Region, Department, SUM(Profit) AS Profit,

GROUPING (Time) as T,

GROUPING (Region) as R,

GROUPING (Department) as D

FROM Sales

GROUP BY ROLLUP (Time, Region, Department);

Time Region Department Profit T R D

2003 Central Video Rental 75,000 0 0 0

2003 Central Video Sales 74,000 0 0 0

2003 Central [NULL] 149,000 0 0 1

2003 East Video Rental 89,000 0 0 0

2003 East Video Sales 115,000 0 0 0

2003 East [NULL] 204,000 0 0 1

2003 West Video Rental 87,000 0 0 0

2003 West Video Sales 86,000 0 0 0

Page 16: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 16

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Time Region Department Profit T R D

2003 West [NULL] 173,000 0 0 1

2003 [NULL] [NULL] 526,000 0 1 1

2004 Central Video Rental 82,000 0 0 0

2004 Central Video Sales 85,000 0 0 0

2004 Central [NULL] 167,000 0 0 1

2004 East Video Rental 101,000 0 0 0

2004 East Video Sales 137,000 0 0 0

2004 East [NULL] 238,000 0 0 1

2004 West VideoRental 96,000 0 0 0

2004 West VideoSales 97,000 0 0 0

2004 West [NULL] 193,000 0 0 1

2004 [NULL] [NULL] 598,000 0 1 1

[NULL] [NULL] [NULL] 1,124,000 1 1 1

T R D GROUPING

"0 0 0"

"0 0 1" 2

"0 1 1" "1 1 1"

GROUPING HAVING

GROUPING_ID

GROUP BY GROUP BY

GROUPING GROUPING

GROUP BY GROUPING

4 GROUP BY 4

GROUPING SQL

GROUPING_ID

GROUPING_ID GROUP BY 1

GROUPING_ID 1 0

GROUP BY

" "

GROUPING_ID

10 CUBE(a, b)

Page 17: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 17

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

GROUPING_ID

a, b 0 0 0

a 0 1 1

b 1 0 2

1 1 3

GROUPING_ID

GROUP_ID

GROUP BY

GROUP_ID

GROUP_ID 0

1

GROUP_ID

GROUP_ID

SELECT Region, State, sum(sales) as sum_sales,

GROUPING_ID(state, region),

GROUP_ID()

FROM salesTable

GROUP BY GROUPING SETS (region, ROLLUP(region, state));

region, state region region

region 2

GROUPING_ID GROUP_ID

Region State sum_sales GROUPING_ID GROUP_ID

E NY 1000 0 0

W CA 2000 0 0

E [NULL] 1000 2 0

E [NULL] 1000 2 1

W [NULL] 2000 2 0

W [NULL] 2000 2 1

[NULL] [NULL] 3000 3 0

Page 18: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 18

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

HAVING GROUP_ID() = 0

region

MV

Oracle Database 10g GROUP BY

CUBE ROLLUP GROUPING

SET 1

Oracle Database

10g

ROLLUP

ROLLUP

CREATE MATERIALIZED VIEW mv AS

SELECT productline, productfamily, productid, region,

state, city, sum(sales) as sum_sales,

grouping_id(productline,

productfamily,productid,region,state,city)

FROM salesTable

GROUP BY productline, ROLLUP(productfamily,

productid), region, ROLLUP(state, city);

SQL 9

1

• productline, productfamily, productid, region, state, city

• productline, productfamily, productid, region, state

• productline, productfamily, productid, region

• productline, productfamily, region, state, city

• productline, productfamily, region, state

• productline, productfamily, region

• productline, region, state, city

• productline, region, state

• productline, region

Page 19: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL 19

Oracle Corporation SQL Aggregation for Business Intelligence in Oracle Database 10g

Oracle

Database

Oracle Database 10g Release 2

Oracle ROLLUP CUBE

GROUPING SET

Oracle

grouping_id

SQL

GROUP BY

Oracle Database 10g SQL

Oracle Database 10g

Page 20: Oracle Database 10g SQL - otndnld.oracle.co.jpotndnld.oracle.co.jp/products/database/oracle10g/bi/pdf/twp_bi_sql... · Oracle Database 10g SQL 3 Oracle Corporation SQL Aggregation

Oracle Database 10g SQL

2005 6

: John Haydu, Sankar Subramanian

:

Oracle Corporation

World Headquarters

500 Oracle Parkway

Redwood Shores, CA 94065

U.S.A.

:

: +1.650.506.7000

: +1.650.506.7200

www.oracle.com

Copyright © 2005, Oracle. All rights reserved.

Oracle JD Edwards PeopleSoft Oracle Corporation