advantages of sql include that it is easy to learn, non ... papers/ada/notes/lect_cb... ·...

24

Upload: others

Post on 23-Jan-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 2: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Advantages of SQL include that it is easy to learn, non-procedural, free-

format,

DBMS-independent, and that it is a recognized international standard.

However, major limitation of SQL is the inability to answer routinely asked

business queries such as computing the percentage change in values between

this

month and a year ago or to compute moving averages, cumulative sums, and

other statistical functions.

Page 3: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 4: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

SELECT location.region_code, SUM(cost)/1000000000 AS total_revenue

FROM sales, time,location

WHERE sales.time_code = time.time_code

AND sales.store_code = location.store_code

AND time.year = 2006

GROUP BY location.region_code

--you could constrain the regions you interested in by using the HAVING

clause.

HAVING LOCATION.REGION_CODE=‘NE’

OR location.region_code=‘NW’

Page 5: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Aggregation is a fundamental part of OLAP. To improve aggregation capabilities

the SQL standard provides extensions to the GROUP BY clause such as the

ROLLUP and CUBE functions.

ROLLUP supports calculations using aggregations such as SUM, COUNT,

MAX, MIN, and AVG at increasing levels of aggregation, from the most detailed

up to a grand total. ROLLUP creates subtotals that roll up from the most detailed

level to a grand total, following a column list specified in the ROLLUP clause.

ROLLUP first calculates the standard aggregate values specified in the GROUP

BY clause and then creates progressively higher level subtotals, moving from

right to left through the column list until finally completing with a grand total.

ROLLUP creates subtotals at n + 1 levels, where n is the number of grouping

columns. For instance, if a query specifies ROLLUP on grouping columns of

store_name, , month, and description (product) (n = 3), the result set will include

rows at 4 aggregation levels.

Page 6: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 7: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 8: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Group BY provides one level of aggregation based on the Group By columns

location.region _code and time.quarter

Page 9: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 10: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

You will note there are additional rows known as super aggregate rows as they

further aggregate rows already aggregated by the values in each grouping

within the grouped table. The effect is to produce progressive subtotals

moving from right to left

GROUP BY ROLLUP clause equivalent to the OLAP operation of roll-ups

NOTE the use of NULLS to denote a super aggregate row

How do we differentiate a super aggregate row and an aggregate row where

the value of a grouping column is unknown and hence NULL? This can be

done using the grouping GROUPING function.

Page 11: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 12: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

When the Grouping function takes a single argument, a column reference and

returns either a 0 or 1. When the grouping column identified by the argument

has a NULL value because the row is a super-aggregate row, the GROUPING

function returns 1 ELSE returns 0

Page 13: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

CUBE is similar to ROLLUP, enabling a single statement to calculate all

possible

combinations of aggregations. CUBE can generate the information needed in

cross-tabulation reports with a single query. CUBE is typically most suitable

in queries that use columns from multiple dimensions rather than columns

representing different levels of a single dimension.

CUBE generates all the subtotals that could be calculated for a data cube with

the specified dimensions.

CUBE can be used in any situation requiring cross-tabular reports. The data

needed for cross-tabular reports can be generated with a single SELECT using

CUBE. Like ROLLUP, CUBE can be helpful in generating summary tables.

However, CUBE can produce a heavy processing load. If you have n

grouping columns for CUBE, this will generate 2^n aggregate levels e.g. with

3 grouping columns, CUBE will generate 8 aggregation levels.

Partial Rollups and Cubes are also supported

GROUP BY location.region_code, ROLLUP(time.quarter)

GROUP BY location.region_code, CUBE(time.quarter)

Composite Columns

GROUP BY ROLLUP (time.year, (time.quarter, time.month),

time.day)

GROUP BY CUBE (time.year, (time.quarter, time.month), time.day)

Page 14: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Generates additional super aggregate rows that are created for every

combination of the grouping columns

Page 15: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

The effect of the GROUP BY CUBE with n grouping columns is equivalent

To the OLAP operation of computing the lattice of cuboids for the n-D cube

Page 16: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Cumulative and moving aggregates

SUM AVG MIN MAX, COUNT, VARIANCE, STDDEV,

FIRST_VALUE, LAST_VALUE

Page 17: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

The difference between RANK and DENSE_RANK is that DENSE_RANK

leaves no gaps in the sequential ranking sequence when there are ties for a

ranking.

Page 18: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 19: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 20: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 21: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 22: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the

Windowing Calculations is another elementary OLAP Operator.

Can be used to compute cumulative, moving, and centered aggregates. They return a value for each row in the table, which depends on other rows in the corresponding window. These aggregate functions provide access to more than one row of a table without a self-join and can be used only in the SELECT and ORDER BY clauses of the query.

Page 23: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the
Page 24: Advantages of SQL include that it is easy to learn, non ... Papers/ADA/notes/lect_cb... · Advantages of SQL include that it is easy to learn, non-procedural, free-format, ... the