rolaps, rollups and cubes: an introduction to super groups cs240a notes from a complete guide to db2...

17
ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann Publishers, Inc., 1998

Upload: jason-chandler

Post on 08-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

ROLLUP SELECT state, avg(income) AS avg_income FROM census GROUP BY ROLLUP(state)

TRANSCRIPT

Page 1: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

ROLAPs, Rollups and Cubes:an Introduction to Super Groups

CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann Publishers, Inc., 1998

Page 2: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

The Census Database

Page 3: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

ROLLUP

SELECT state, avg(income) AS avg_incomeFROM censusGROUP BY ROLLUP(state)

Page 4: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

ROLLUP: a more complex example

The total population and the average income in each city, county, and state, and the census as a whole:

SELECT state, county, citycount(*) AS population,

avg(income) AS avg_income FROM census GROUP BY ROLLUP (state, county, city);

Page 5: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

STATE

FL FL FL FL FL TX TX TX TX FL FL TX TX FL TX

(null)

COUNTY

Dade Dade Dade

Orange Orange Harris Harris Travis Travis Dade

Orange Harris Travis

(null) (null) (null)

CITY

Hialeah Miami (null)

Orlando Taft

Baytown Houston Austin (null) (null) (null) (null) (null) (null) (null) (null)

POPULATION

2 2 2 2 2 2 3 2 1 6 4 5

3 10 8

18

AVG_INCOME

38700 36150 32950 42350 29550 30650 37800 40450 34800 35933 35950 34225 38566 35940 36085 36000

ROLLUP more complex example: Result

Page 6: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Getting Rid of nulls by using the grouping(A) function

SELECT CASE grouping(state) WHEN 1 THEN ‘(-all-)’ ELSE state END AS state,CASE grouping(county) WHEN 1 THEN ‘(-all-)’ ELSE county END AS county,CASE grouping(city) WHEN 1 THEN ‘(-all-)’ ELSE city END AS city,count(*) AS pop, avg(income) AS avg_incomeFROM censusGROUP BY ROLLUP(state, count, city);

Page 7: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Result of getting rid of nullsSTATE

FL FL FL FL FL TX TX TX TX FL FL TX TX FL TX

(-all-)

COUNTY

Dade Dade Dade

Orange Orange Harris Harris Travis Travis Dade

Orange Harris Travis (-all-)

(-all-) (-all-)

CITY

Hialeah Miami (null)

Orlando Taft

Baytown Houston Austin (null) (-all-) (-all-) (-all-) (-all-) (-all-) (-all-) (-all-)

POPULATION

2 2 2 2 2 2 3 2 1 6 4 5

3 10 8

18

AVG_INCOME

38700 36150 32950 42350 29550 30650 37800 40450 34800 35933 35950 34225 38566 35940 36085 36000

Page 8: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Using WHERE & HAVING in ROLLUPsSELECT CASE grouping(state) WHEN 1 THEN ‘(-all-)’ ELSE state END AS state, CASE grouping(county) WHEN 1 THEN ‘(-all-)’ ELSE county END AS county, CASE grouping(city)

WHEN 1 THEN ‘(-all-)’ ELSE city END AS city, count(*) AS pop, avg(income) AS avg_incomeFROM censusWHERE sex= ‘F ’GROUP BY ROLLUP(state, count, city)HAVING count(*) >=2;

Page 9: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Result of WHERE and HAVING on ROLLUP

Page 10: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

The CUBEEffect of sex and

birth date on income. Four possible ways to group:

SELECT sex, year(brirthdate) AS birth_year

max(income) AS max_incomeFROM censusGROUP BY CUBE(sex, year(birthdate));

1. by sex and birthdate,2. by sex only3. By birthdate only 4. By nothing—the table

becomes one big group

Page 11: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Result of Cubing on Sex and Birth Year

Page 12: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

A 3-D Cube: size and avg income for groups of size 4

SELECT CASE grouping(state)WHEN 1 THEN ‘(-all)’ ELSE state END AS state,

CASE grouping(sex)WHEN 1 THEN ‘(-all)’ ELSE sex END AS sex,

CASE grouping(year(brirthdate))WHEN 1 THEN ‘(-all)’

ELSE char(year(brirthdate)) END AS birth_datecount(*) AS count, avg(income) AS avg_incomeFROM censusGROUP BY CUBE(state, sex, year(birthdate))HAVING count(*) >= 4;

Page 13: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Size and Income for groups of size 4:result

Page 14: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Grouping SetsSELECT CASE grouping(state) WHEN 1 THEN ‘(-all-)’ ELSE state END AS state, CASE grouping(sex)

WHEN 1 THEN ‘(-all-)’ ELSE sex END AS sex, CASE grouping(year(birthdate)) WHEN 1 THEN ‘(-all-)’ ELSE char(year(birthdate)) END AS birth_year,

count(*) AS count, avg(income) AS avg_income

FROM censusGROUP BY GROUPING SETS (state, sex), year(birthdate), ();

Page 15: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Result of Grouping Sets

Page 16: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Multiple Grouping Specifications

SELECT CASE grouping(state) WHEN 1 THEN ‘(-all-)’ ELSE state END AS state, CASE grouping(county) WHEN 1 THEN ‘(-all-)’ ELSE county END AS county, sex,

count(*) AS pop, avg(income) AS avg_income

FROM censusGROUP BY ROLLUP(state, county), sex;

Page 17: ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2 Universal Database, by Don Chamberlin, Morgan Kaufmann

Result of Multiple Grouping

STATE

FL FL FL FL FL FL TX TX TX TX TX TX

(-all-) (-all-)

COUNTY

Dade Dade

Orange Orange (-all-) (-all-) Harris Harris Travis Travis (-all-) (-all-) (-all-) (-all-)

SEX

F M F M F M F M F M F M F M

POP

2 4 2 2 4 6 2 3 1 2 3 5

7 11

AVG_INCOME

40100 33850 36600 35300 38350 34333 44700 30733 34800 40450 39750 34620 38816 34463