SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Basic Spreadsheet Functions Objective Functions are predefined formulas that perform calculations by using specific values, called arguments, in.
Database Systems: Design, Implementation, and Management Tenth Edition
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Part I Chapter Three CIS 218.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Mary K. Olson PS Reporting Instance – Query Tool 101.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Copyright © Curt Hill Queries in SQL More options.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IFS180 Intro. to Data Management Chapter 11 - Subqueries.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Aggregating Data Using Group Functions
SQL AGGREGATE FUNCTIONS
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
The Database Exercises Fall, 2009.
SQL FUNDAMENTALS CDSE Days 2018.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Access: SQL Participation Project
Reporting Aggregated Data Using the Group Functions
Query Functions.
Access: Queries III Participation Project
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Introduction to SQL Server and the Structure Query Language
Group Operations Part IV.
Presentation transcript:

SQL Aggregation Oracle and ANSI Standard SQL Lecture 9

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

SQL Aggregation COUNT() Function COUNT({ * | [DISTINCT|ALL] | expression }) Copyright 2006 Page 3

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

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

SQL Aggregation COUNT() Function: Counts a row SELECT COUNT(*) FROM a_table a; Copyright 2006 Page 6

SQL Aggregation COUNT() Function: Counts values SELECT COUNT(a.column_name) FROM a_table a; Copyright 2006 Page 7

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) row selected. Copyright 2006 Page 8

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

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) rows selected. Copyright 2006 Page 10

SQL Aggregation SUM() Function SUM({ [DISTINCT|ALL] | value }) SUM({ [DISTINCT|ALL] | formula }) Copyright 2006 Page 11

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

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

SQL Aggregation SUM() Function: Sums a row SELECT SUM(a.column_name) FROM a_table a; Copyright 2006 Page 14

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) row selected. Copyright 2006 Page 15 SQL Aggregation SUM() Function: Sums values

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

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) rows selected. Copyright 2006 Page 17 SQL Aggregation SUM() Function: Sums values

SQL Aggregation AVG() Function AVG({ [DISTINCT|ALL] | value }) AVG({ [DISTINCT|ALL] | formula }) Copyright 2006 Page 18

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

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

SQL Aggregation AVG() Function: Average a column value SELECT AVG(a.column_name) FROM a_table a; Copyright 2006 Page 21

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) row selected. Copyright 2006 Page 22 SQL Aggregation AVG() Function: Average a column value

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

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) rows selected. Copyright 2006 Page 24

SQL Aggregation MAX() Function MAX({ [DISTINCT|ALL] | expression }) OVER (PARTITION BY expression) Copyright 2006 Page 25

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

SQL Aggregation MAX() Function: Finds maximum value SELECT MAX(a.column_name) FROM a_table a; Copyright 2006 Page 27

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) row selected. Copyright 2006 Page 28

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

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

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) rows selected. Copyright 2006 Page 31

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

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) ST Set 9 1ST Set 9 2ND Set 9 2ND Set 9 2 rows selected. Copyright 2006 Page 33

SQL Aggregation MIN() Function MIN({ [DISTINCT|ALL] | expression }) OVER (PARTITION BY expression) Copyright 2006 Page 34

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

SQL Aggregation MIN() Function: Finds minimum value SELECT MIN(a.column_name) FROM a_table a; Copyright 2006 Page 36

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) row selected. Copyright 2006 Page 37

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

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

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) rows selected. Copyright 2006 Page 40

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

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) ST Set 0 1ST Set 0 2ND Set 0 2ND Set 0 2 rows selected. Copyright 2006 Page 42

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

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

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

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

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

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

SQL Aggregation ORDER BY Clause SELECT a.column_name1, a.column_name2 FROM a_table a ORDER BY 1, 2; Copyright 2006 Page 49

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