ROLAPs, Rollups and Cubes: an Introduction to Super Groups CS240A Notes from A Complete Guide to DB2...

Preview:

DESCRIPTION

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

Citation preview

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

The Census Database

ROLLUP

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

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);

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

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);

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

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;

Result of WHERE and HAVING on ROLLUP

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

Result of Cubing on Sex and Birth Year

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;

Size and Income for groups of size 4:result

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), ();

Result of Grouping Sets

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;

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