Chapter 10 - Aggregation

“Matrix climbed Aggregate Mountain and delivered a better way to Sum It.”

- Tera-Tom Coffing

Quiz – You calculate the Answer Set in your own Mind

Aggregation_Table

Employee_NoSalary

__________________________

423400100000.00

423401100000.00

423402NULL

SELECT  AVG(Salary)    as "AVG"

Count(Salary) as SalCnt

‚Count(*)         as RowCnt

FROM    Aggregation_Table ;

image

What would the result set be from the above query? The next slide shows answers!

Answer – You calculate the Answer Set in your own Mind

image

SELECT  AVG(Salary)    as "AVG"

Count(Salary) as SalCnt

‚Count(*)         as RowCnt

FROM    Aggregation_Table ;

image

Here are your answers!

The 3 Rules of Aggregation

image

1) Aggregates Ignore Null Values.

2) Aggregates WANT to come back in one row.

3) You CAN’T mix Aggregates with normal columns unless you use a GROUP BY.

image

There are Five Aggregates

image

The five aggregates are listed above.

Quiz – How many rows come back?

image

How many rows will the above query produce in the result set?

Answer – How many rows come back?

image

How many rows will the above query produce in the result set? The answer is one.

Troubleshooting Aggregates

image

If you have a normal column (non aggregate) in your query, you must have a corresponding GROUP BY statement.

GROUP BY when Aggregates and Normal Columns Mix

image

If you have a normal column (non aggregate) in your query, you must have a corresponding GROUP BY statement.

GROUP BY delivers one row per Group

image

Group By Dept_No command allow for the Aggregates to be calculated per Dept_No. The data has also been sorted with the ORDER BY statement.

GROUP BY Dept_No or GROUP BY 1 the same thing

image

Both queries above produce the same result. The GROUP BY allows you to either name the column or use the number in the SELECT list just like the ORDER BY.

Limiting Rows and Improving Performance with WHERE

image

Will Dept_No 300 be calculated? Of course you know it will . . . NOT!

WHERE Clause in Aggregation limits unneeded Calculations

image

The system eliminates reading any other Dept_No’s other than 200 and 400. This means that only Dept_No’s of 200 and 400 will come off the disk to be calculated.

Keyword HAVING tests Aggregates after they are Totaled

image

The HAVING Clause only works on Aggregate Totals. The WHERE filters rows to be excluded from calculation, but the HAVING filters the Aggregate totals after the calculations, thus eliminating certain Aggregate totals.

Keyword HAVING is like an Extra WHERE Clause for Totals

image

The HAVING Clause only works on Aggregate Totals, and in the above example, only Count(*) > 2 can return.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.239.111