Download presentation
Presentation is loading. Please wait.
Published byPhilomena White Modified over 9 years ago
1
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9
2
SQL Aggregation COUNT() function COUNT() function SUM() function SUM() function AVG() function AVG() function MIN() function MIN() function MAX() function MAX() function GROUP BY clause GROUP BY clause HAVING clause HAVING clause ORDER BY clause ORDER BY clause Copyright 2006 Page 2
3
SQL Aggregation COUNT() Function COUNT({ * | [DISTINCT|ALL] | expression }) Copyright 2006 Page 3
4
SQL Aggregation COUNT() Function COUNT() function COUNT() function The COUNT() function counts a set of things based on it being the only column value selected: The COUNT() function counts a set of things based on it being the only column value selected: Returning only one column and row with the total number of rows found. Returning only one column and row with the total number of rows found. The COUNT() function counts a set of things based on a criteria specified in a GROUP BY clause and more than one column is selected: The COUNT() function counts a set of things based on a criteria specified in a GROUP BY clause and more than one column is selected: Returning more than one row when there is more than one row of the grouping column, and Returning more than one row when there is more than one row of the grouping column, and Returning the count of how many times the grouping columns occur in the set. Returning the count of how many times the grouping columns occur in the set. Copyright 2006 Page 4
5
SQL Aggregation COUNT() Function COUNT(*) COUNT(*) This approach counts all rows including those with NULL values. This approach counts all rows including those with NULL values. COUNT(*) with a GROUP BY col_name COUNT(*) with a GROUP BY col_name This approach counts all rows uniquely identified by the column name, substituting 1 for all rows identified as unique provided there is only one row containing unique column values. This approach counts all rows uniquely identified by the column name, substituting 1 for all rows identified as unique provided there is only one row containing unique column values. COUNT(col_name) COUNT(col_name) This approach counts all rows excluding those rows that contain a NULL value for the designated column name. This approach counts all rows excluding those rows that contain a NULL value for the designated column name. COUNT(col_name) with a GROUP BY col_name COUNT(col_name) with a GROUP BY col_name This approach counts all rows including rows that contain a NULL value, substituting 1 for not null values and 0 for null values provided there is only one row containing unique column values. This approach counts all rows including rows that contain a NULL value, substituting 1 for not null values and 0 for null values provided there is only one row containing unique column values. Copyright 2006 Page 5
6
SQL Aggregation COUNT() Function: Counts a row SELECT COUNT(*) FROM a_table a; Copyright 2006 Page 6
7
SQL Aggregation COUNT() Function: Counts values SELECT COUNT(a.column_name) FROM a_table a; Copyright 2006 Page 7
8
SQL Aggregation COUNT() Function: Counts values SELECT COUNT(value_list) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Selecting only COUNT(*) from the table returns 22 rows. Selecting only COUNT(*) from the table returns 22 rows. COUNT(VALUE_LIST)----------------- 20 20 1 row selected. Copyright 2006 Page 8
9
SQL Aggregation COUNT() Function: Counts values SELECT a.column_name1, COUNT(a.column_name) FROM a_table a GROUP BY a.column_name2; Copyright 2006 Page 9
10
SQL Aggregation COUNT() Function: Counts values SELECT value_list, COUNT(value_list) FROM counting GROUP BY value_list; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Selecting only COUNT(*) from the table returns 22 rows. Selecting only COUNT(*) from the table returns 22 rows. VALUE_LIST COUNT(VALUE_LIST) ---------- ----------------- 0 2 0 2 1 2 1 2 2 2 2 2 3 2 3 2 4 2 4 2 5 2 5 2 6 2 6 2 7 2 7 2 8 2 8 2 9 2 9 2 0 11 rows selected. Copyright 2006 Page 10
11
SQL Aggregation SUM() Function SUM({ [DISTINCT|ALL] | value }) SUM({ [DISTINCT|ALL] | formula }) Copyright 2006 Page 11
12
SQL Aggregation SUM() Function SUM() function SUM() function The SUM() function sums a set of things based on it being the only column value selected: The SUM() function sums a set of things based on it being the only column value selected: Returning only one column and row with the sum of a set of column values. Returning only one column and row with the sum of a set of column values. The SUM() function sums a set of things based on a criteria specified in a GROUP BY clause and more than one column is selected: The SUM() function sums a set of things based on a criteria specified in a GROUP BY clause and more than one column is selected: Returning more than one row when there is more than one row of the grouping column, and Returning more than one row when there is more than one row of the grouping column, and Returning the count of how many times the grouping columns occur in the set. Returning the count of how many times the grouping columns occur in the set. Copyright 2006 Page 12
13
SQL Aggregation SUM() Function The SUM() function requires a NUMBER data type or subtype. The SUM() function requires a NUMBER data type or subtype. SUM(column_value) SUM(column_value) This approach adds all rows values for a column name. This approach adds all rows values for a column name. SUM(formula returing value) SUM(formula returing value) This approach adds all rows based on the formula. This approach adds all rows based on the formula. SUM() functions can be used in: SUM() functions can be used in: The SELECT and HAVING clauses. The SELECT and HAVING clauses. Copyright 2006 Page 13
14
SQL Aggregation SUM() Function: Sums a row SELECT SUM(a.column_name) FROM a_table a; Copyright 2006 Page 14
15
SELECT SUM(value_list) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. SUM(VALUE_LIST)--------------- 90 90 1 row selected. Copyright 2006 Page 15 SQL Aggregation SUM() Function: Sums values
16
SQL Aggregation SUM() Function: Sums a row SELECT a.column_name1, SUM(a.column_name2) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 16
17
SELECT value_list, SUM(value_list) FROM counting GROUP BY value_list; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Grouping by the VALUE_LIST value, the sum is twice the value in the column. Grouping by the VALUE_LIST value, the sum is twice the value in the column. VALUE_LIST SUM(VALUE_LIST) ---------- --------------- 0 0 0 0 1 2 1 2 2 4 2 4 3 6 3 6 4 8 4 8 5 10 5 10 6 12 6 12 7 14 7 14 8 16 8 16 9 18 9 18 11 rows selected. Copyright 2006 Page 17 SQL Aggregation SUM() Function: Sums values
18
SQL Aggregation AVG() Function AVG({ [DISTINCT|ALL] | value }) AVG({ [DISTINCT|ALL] | formula }) Copyright 2006 Page 18
19
SQL Aggregation AVG() Function The AVG() function averages a set of things and returns only one row when it is the only return value in a SELECT clause. The AVG() function averages a set of things and returns only one row when it is the only return value in a SELECT clause. The AVG() function averages a set of things based on a criteria specified in a GROUP BY clause and returns more than one row – the grouping attribute and the average of their occurrences in the set. The AVG() function averages a set of things based on a criteria specified in a GROUP BY clause and returns more than one row – the grouping attribute and the average of their occurrences in the set. Copyright 2006 Page 19
20
SQL Aggregation AVG() Function The AVG() function requires a NUMBER data type or subtype. The AVG() function requires a NUMBER data type or subtype. AVG(column_value) AVG(column_value) This approach averages all rows values for a column name. This approach averages all rows values for a column name. AVG(formula returing value) AVG(formula returing value) This approach averages all rows based on the formula. This approach averages all rows based on the formula. AVG() functions can be used in: AVG() functions can be used in: The SELECT and HAVING clauses. The SELECT and HAVING clauses. Copyright 2006 Page 20
21
SQL Aggregation AVG() Function: Average a column value SELECT AVG(a.column_name) FROM a_table a; Copyright 2006 Page 21
22
SELECT AVG(value_list) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. AVG(VALUE_LIST)--------------- 4.5 4.5 1 row selected. Copyright 2006 Page 22 SQL Aggregation AVG() Function: Average a column value
23
SQL Aggregation AVG() Function: Average a column values SELECT a.column_name1, AVG(a.column_name2) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 23
24
SQL Aggregation AVG() Function: Average a column values SELECT value_list, AVG(value_list) FROM counting GROUP BY value_list; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Grouping by the VALUE_LIST value, the average is the value in the column. Grouping by the VALUE_LIST value, the average is the value in the column. VALUE_LIST AVG(VALUE_LIST) ---------- --------------- 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 11 rows selected. Copyright 2006 Page 24
25
SQL Aggregation MAX() Function MAX({ [DISTINCT|ALL] | expression }) OVER (PARTITION BY expression) Copyright 2006 Page 25
26
SQL Aggregation MAX() Function The MAX() function: The MAX() function: Requires a scalar type or subtype Requires a scalar type or subtype Can use an analytical function, like PARTITION BY Can use an analytical function, like PARTITION BY MAX(column_name) MAX(column_name) This approach finds the highest value of an expression, ASCII values are used for strings. This approach finds the highest value of an expression, ASCII values are used for strings. NULL are sorted last in ascending order and first in descending order. NULL are sorted last in ascending order and first in descending order. MAX(column_name) OVER (PARTITION BY column_name) MAX(column_name) OVER (PARTITION BY column_name) This approach finds the highest value of an expression based on its relationship in the result set to the partitioning column, ASCII values are used for strings. This approach finds the highest value of an expression based on its relationship in the result set to the partitioning column, ASCII values are used for strings. NULL are sorted last in ascending order and first in descending order. NULL are sorted last in ascending order and first in descending order. Copyright 2006 Page 26
27
SQL Aggregation MAX() Function: Finds maximum value SELECT MAX(a.column_name) FROM a_table a; Copyright 2006 Page 27
28
SQL Aggregation MAX() Function: Finds maximum value SELECT MAX(value_list) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. It returns the highest number without any grouping or partitioning. It returns the highest number without any grouping or partitioning. MAX(VALUE_LIST)--------------- 9 1 row selected. Copyright 2006 Page 28
29
SQL Aggregation MAX() Function: Finds maximum value SELECT MAX(a.column_name1) OVER (PARTITION BY a.column_name2) OVER (PARTITION BY a.column_name2) FROM a_table a; Copyright 2006 Page 29
30
SQL Aggregation MAX() Function: Finds maximum value SELECT a.column_name1, MAX(a.column_name2) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 30
31
SQL Aggregation MAX() Function: Finds Maximum Value SELECT value_list, MAX(value_list) FROM counting GROUP BY value_list; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Grouping by the VALUE_LIST value, the maximum is the value in the column. Grouping by the VALUE_LIST value, the maximum is the value in the column. VALUE_LIST MAX(VALUE_LIST) ---------- --------------- 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 11 rows selected. Copyright 2006 Page 31
32
SQL Aggregation MAX() Function: Finds maximum value SELECT DISTINCT a.column_name1 a.column_name1, MAX(a.column_name2) OVER (PARTITION BY a.column_name1) OVER (PARTITION BY a.column_name1) FROM a_table a; Copyright 2006 Page 32
33
SQL Aggregation MAX() Function: Finds maximum value SELECT DISTINCT value_name value_name, MAX(value_list) OVER (PARTITION BY value_name) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. The OVER clause disallows the use of the GROUP BY clause and a DISTINCT provides meaningful results. The OVER clause disallows the use of the GROUP BY clause and a DISTINCT provides meaningful results. VALUE_NAME MAX(VALUE_LIST) ---------- --------------- 1ST Set 9 1ST Set 9 2ND Set 9 2ND Set 9 2 rows selected. Copyright 2006 Page 33
34
SQL Aggregation MIN() Function MIN({ [DISTINCT|ALL] | expression }) OVER (PARTITION BY expression) Copyright 2006 Page 34
35
SQL Aggregation MIN() Function The MIN() function: The MIN() function: Requires a scalar type or subtype Requires a scalar type or subtype Can use an analytical function, like PARTITION BY Can use an analytical function, like PARTITION BY MIN(column_name) MIN(column_name) This approach finds the lowest value of an expression, ASCII values are used for strings. This approach finds the lowest value of an expression, ASCII values are used for strings. NULL are sorted last in ascending order and first in descending order. NULL are sorted last in ascending order and first in descending order. MIN(column_name) OVER (PARTITION BY column_name) MIN(column_name) OVER (PARTITION BY column_name) This approach finds the lowest value of an expression based on its relationship in the result set to the partitioning column, ASCII values are used for strings. This approach finds the lowest value of an expression based on its relationship in the result set to the partitioning column, ASCII values are used for strings. NULL are sorted first in ascending order and last in descending order. NULL are sorted first in ascending order and last in descending order. Copyright 2006 Page 35
36
SQL Aggregation MIN() Function: Finds minimum value SELECT MIN(a.column_name) FROM a_table a; Copyright 2006 Page 36
37
SQL Aggregation MIN() Function: Finds minimum value SELECT MIN(value_list) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. It returns the lowest number without any grouping or partitioning. It returns the lowest number without any grouping or partitioning. MIN(VALUE_LIST)--------------- 9 1 row selected. Copyright 2006 Page 37
38
SQL Aggregation MIN() Function: Finds minimum value SELECT MAX(a.column_name1) OVER (PARTITION BY a.column_name2) OVER (PARTITION BY a.column_name2) FROM a_table a; Copyright 2006 Page 38
39
SQL Aggregation MIN() Function: Finds minimum value SELECT a.column_name1, MIN(a.column_name2) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 39
40
SQL Aggregation MIN() Function: Finds minimum value SELECT value_list, MIN(value_list) FROM counting GROUP BY value_list; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. Grouping by the VALUE_LIST value, the minimum is the value in the column. Grouping by the VALUE_LIST value, the minimum is the value in the column. VALUE_LIST MIN(VALUE_LIST) ---------- --------------- 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 11 rows selected. Copyright 2006 Page 40
41
SQL Aggregation MIN() Function: Finds minimum value SELECT a.column_name1, MIN(a.column_name2) OVER (PARTITION BY a.column_name1) OVER (PARTITION BY a.column_name1) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 41
42
SQL Aggregation MIN() Function: Finds minimum value SELECT DISTINCT value_name value_name, MIN(value_list) OVER (PARTITION BY value_name) FROM counting; Contains two copies of the single digit ordinal numbers, and two null values. Contains two copies of the single digit ordinal numbers, and two null values. The OVER clause disallows the use of the GROUP BY clause and a DISTINCT provides meaningful results. The OVER clause disallows the use of the GROUP BY clause and a DISTINCT provides meaningful results. VALUE_NAME MIN(VALUE_LIST) ---------- --------------- 1ST Set 0 1ST Set 0 2ND Set 0 2ND Set 0 2 rows selected. Copyright 2006 Page 42
43
SQL Aggregation GROUP BY Clause The GROUP BY clause lets you group a result set by a condition, column value, or combination of both. The GROUP BY clause lets you group a result set by a condition, column value, or combination of both. The GROUP BY clause works: The GROUP BY clause works: After any WHERE clause After any WHERE clause Once for any query component, and can be different for two queries joined by a set operator into a master query Once for any query component, and can be different for two queries joined by a set operator into a master query May follow or precede the HAVING clause May follow or precede the HAVING clause The GROUP BY works on data type rules and standard operator precedence. The GROUP BY works on data type rules and standard operator precedence. Copyright 2006 Page 43
44
SQL Aggregation GROUP BY Clause SELECT a.column_name1, SUM(a.column_name2) FROM a_table a GROUP BY a.column_name1; Copyright 2006 Page 44
45
SQL Aggregation HAVING Clause The HAVING clause lets you group a result set by evaluating an expression, which can be a aggregation function result compared to a literal. The HAVING clause lets you group a result set by evaluating an expression, which can be a aggregation function result compared to a literal. The HAVING clause works: The HAVING clause works: After any WHERE clause After any WHERE clause Once for any query component, and can be different for two queries joined by a set operator into a master query Once for any query component, and can be different for two queries joined by a set operator into a master query May follow or precede the GROUP BY clause May follow or precede the GROUP BY clause The HAVING clause works using SQL comparison operators. The HAVING clause works using SQL comparison operators. Copyright 2006 Page 45
46
SQL Aggregation HAVING BY Clause SELECT a.column_name1, a.column_name2 FROM a_table a HAVING COUNT(a.column_name3) > 1; Copyright 2006 Page 46
47
SQL Aggregation ORDER BY Clause The ORDER BY clause lets you sort a result set. The ORDER BY clause lets you sort a result set. The ORDER BY clause works: The ORDER BY clause works: After any GROUP BY clause After any GROUP BY clause Only once for a set of queriers joined by one or more set operators into a master query Only once for a set of queriers joined by one or more set operators into a master query By following the GROUP BY clause By following the GROUP BY clause The ORDER BY works on data type rules: The ORDER BY works on data type rules: The DATE type is sorted by the numeric value of the timestamp. The DATE type is sorted by the numeric value of the timestamp. The VARCHAR2 type is sorted by ASCII values, a ' JAN ' and ' FEB ' from a TO_CHAR(date_column, ' MON ' ) will sort ' FEB ' first. The VARCHAR2 type is sorted by ASCII values, a ' JAN ' and ' FEB ' from a TO_CHAR(date_column, ' MON ' ) will sort ' FEB ' first. Copyright 2006 Page 47
48
SQL Aggregation ORDER BY Clause SELECT a.column_name1, a.column_name2 FROM a_table a ORDER BY a.column_name1, a.column_name2; Copyright 2006 Page 48
49
SQL Aggregation ORDER BY Clause SELECT a.column_name1, a.column_name2 FROM a_table a ORDER BY 1, 2; Copyright 2006 Page 49
50
Summary COUNT() function COUNT() function SUM() function SUM() function AVG() function AVG() function MIN() function MIN() function MAX() function MAX() function GROUP BY clause GROUP BY clause HAVING clause HAVING clause ORDER BY clause ORDER BY clause Copyright 2006 Page 50
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.