Chapter 17

Aggregation Function

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

Tera-Tom Coffing

Table of Contents Chapter 17 – Aggregation Function

Quiz – You calculate the Answer Set in your own Mind

Answer – You calculate the Answer Set in your own Mind

The 3 Rules of Aggregation

There are Five Aggregates

Quiz – How many rows come back?

Troubleshooting Aggregates

GROUP BY when Aggregates and Normal Columns Mix

GROUP BY Delivers one row per Group

GROUP BY Dept_No or GROUP BY 1 the same thing

Limiting Rows and Improving Performance with WHERE

WHERE Clause in Aggregation limits unneeded Calculations

Keyword HAVING tests Aggregates after they are Totaled

Keyword HAVING is like an Extra WHERE Clause for Totals

Three types of Advanced Grouping

GROUP BY Grouping Sets

GROUP BY Rollup

GROUP BY Rollup Result Set

GROUP BY Cube

GROUP BY CUBE Result Set

Testing Your Knowledge

Testing Your Knowledge

Testing Your Knowledge

Testing Your Knowledge

Testing Your Knowledge

Final Answer to Test Your Knowledge on Aggregates

Quiz – You calculate the Answer Set in your own Mind

Aggregation_Table

Employee_No Salary
423400 100000.00
423401 100000.00
423402 NULL

 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

Aggregation_Table

Employee_No Salary
423400 100000.00
423401 100000.00
423402 NULL

 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!

The 3 Rules of Aggregation

Aggregation_Table

Employee_No Salary
423400 100000.00
423401 100000.00
423402 NULL

 SELECT AVG(Salary)
        ,Count(Salary)
        ,Count(*)
 FROM Aggregation_Table;

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.

 AVG(Salary) = $100000.00

 Count(Salary) = 2

 Count(*) = 3

There are Five Aggregates

Aggregation_Table

Employee_No Salary
423400 100000.00
423401 100000.00
423402 NULL

There are FIVE AGGREGATES which are the following:

MIN – The Minimum Value.

MAX – The Maximum Value.

AVG – The Average of the Column Values.

SUM – The Sum Total of the Column Values.

COUNT – The Count of the Column Values.

Quiz – How many rows come back?

image

image

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

Troubleshooting Aggregates

image

image

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

GROUP BY when Aggregates and Normal Columns Mix

image

image

GROUP BY Delivers one row per Group

image

image

Group By Dept_No command allow for the Aggregates to be calculated per Dept_No.

GROUP BY Dept_No or GROUP BY 1 the same thing

 SELECT Dept_No
        ,MIN (Salary)
        ,MAX (Salary)
        ,SUM (Salary)
        ,AVG (Salary)
        ,Count(*)
 FROM Employee_Table
 GROUP BY Dept_No
 ORDER BY Dept_No;

Both Queries are the same to the system.

 SELECT Dept_No
        ,MIN (Salary)
        ,MAX (Salary)
        ,SUM (Salary)
        ,AVG (Salary)
        ,Count(*)
 FROM Employee_Table
 GROUP BY 1
 ORDER BY 1;

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

image

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

WHERE Clause in Aggregation limits unneeded Calculations

image

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

Previous Answer Set

image

image

The HAVING Clause only works on Aggregate Totals. The WHERE filters rows they don't want calculated, 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

Previous Answer Set

image

NEW Answer Set

image

The HAVING Clause only works on Aggregate Totals and only Count(*) > 2 can return.

Three types of Advanced Grouping

Not all rows in this table are displayed

Sales_Table

 
Product_ID Sale_Date Daily_Sales
1000 2000-09-28 48850.40
2000 2000-09-28 41888.88
3000 2000-09-28 61301.77
1000 2000-09-29 54500.22
2000 2000-09-29 48000.00
3000 2000-09-29 34509.13
1000 2000-09-30 36000.07
2000 2000-09-30 49850.03
3000 2000-09-30 43868.86
1000 2000-10-01 40200.43
2000 2000-10-01 54850.29
3000 2000-10-01 28000.00
1000 2000-10-02 32800.50
2000 2000-10-02 36021.93
3000 2000-10-02 19678.94

GROUP BY Grouping Sets

GROUP BY Rollup

GROUP BY Cube

GROUP BY GROUPING Sets

image

image

GROUP BY GROUPING Sets above will show you what your Daily_Sales were for each Product_ID, for each month, and for each year.

GROUP BY Rollup

image

Not all 16 rows in this answer set could be displayed

image

GROUP BY ROLLUP displays Daily_Sales were for each Product_ID, for each distinct month, for each month per year and for each year, plus a grand total.

GROUP BY Rollup Result Set

image

This is the full result set from the previous GROUP BY ROLLUP query.

GROUP BY Cube

image

Not all 24 rows in this answer set could be displayed

image

GROUP BY ROLLUP displays Daily_Sales were for each Product_ID, for each distinct month, for each month per year and for each year, plus a grand total.

GROUP BY CUBE Result Set

image

Testing Your Knowledge

image

First of all, SELECT the AVERAGE Salary from the Employee_Table.

Testing Your Knowledge

image

Now , SELECT the AVERAGE Salary and the SUM of the Salary from the Employee_Table.

Testing Your Knowledge

image

Now , SELECT the AVERAGE Salary and the SUM of the Salary from the Employee_Table but PER DEPARTMENT(Dept_No).

Testing Your Knowledge

image

After that, SELECT the AVERAGE Salary and the SUM of the Salary from the Employee_Table but PER DEPARTMENT(Dept_No). However, I only want to see the people from Department 200, 300, 400.

Testing Your Knowledge

image

After that, SELECT the AVERAGE Salary and the SUM of the Salary from the Employee_Table but PER DEPTMENT(Dept_No). However, I only want to see Department 200, 300, 400 which has an AVERAGE Salary of over 43,000.

Final Answer to Test Your Knowledge on Aggregates

Answer Set

Dept_No AVG(Salary) Sum(Salary)
200 44944.44 89888.88
400 48316.67 144950.00

Select Dept_No, AVG(Salary), SUM(Salary
From Employee_Table
Where Dept_No IN (200, 300, 400)
Group By Dept_No
Having AVG(Salary) > 43000

This should be your final answer set. The query under it should be approximately what you wrote to attain such an answer set. How'd you do?

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

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