Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS122 Using Relational Databases and SQL

Similar presentations


Presentation on theme: "CS122 Using Relational Databases and SQL"— Presentation transcript:

1 CS122 Using Relational Databases and SQL
2/23/2019 CS122 Using Relational Databases and SQL 3. Aggregate calculations Daniel Firpo Slides prepared by Randy Moss Department of Computer Science California State University, Los Angeles

2 Outline Aggregate calculations for a table
2/23/2019 Outline Aggregate calculations for a table Aggregate calculations for groups Function followed by field or expression in parentheses Can use AS and an alias column name Can use Aggregate as part of expression 3. Aggregate Calculations CS1222_W2018

3 What is an Aggregate? Summarizes values from entire groups of rows
2/23/2019 What is an Aggregate? Summarizes values from entire groups of rows SUM: To calculate totals AVG: To calculate averages COUNT: To count the number of records MIN: To report the minimum value MAX: To report the maximum value 3. Aggregate Calculations CS1222_W2018

4 Sum Add up a column Only for numeric attributes
2/23/2019 Sum Add up a column Only for numeric attributes Any null values will be ignored. E.X. Report the total time in seconds of all tracks SELECT Sum (lengthseconds) FROM Tracks 3. Aggregate Calculations CS1222_W2018

5 2/23/2019 Avg AVG ignores nulls Report the average length in minutes of all tracks SELECT Avg(lengthseconds)/60 FROM Tracks 3. Aggregate Calculations CS1222_W2018

6 Avg 30! SELECT Avg(age) FROM Employees The result is 20 or 30? 01 25
2/23/2019 Avg Employees eid age 01 25 02 NULL 03 35 SELECT Avg(age) FROM Employees The result is 20 or 30? 30! 3. Aggregate Calculations CS1222_W2018

7 Count Take as an argument either a field name or an asterisk(*)
2/23/2019 Count Take as an argument either a field name or an asterisk(*) E.X. Report the number of members in the members table SELECT Count(*) AS NumMembers FROM Members 3. Aggregate Calculations CS1222_W2018

8 Count Difference between Count(*) and Count (field) SELECT Count(age)
2/23/2019 Count Difference between Count(*) and Count (field) SELECT Count(age) FROM Employees Employees 2! eid age 01 25 02 NULL 03 35 SELECT Count(*) FROM Employees 3! 3. Aggregate Calculations CS1222_W2018

9 Min, Max Report the minimum and maximum values.
2/23/2019 Min, Max Report the minimum and maximum values. Can be used with numeric datatypes, dates and text E.X. Report the shortest and the longest track lengths in seconds SELECT Min(lengthseconds) as Shortest, Max(lengthseconds) as Longest FROM Tracks 3. Aggregate Calculations CS1222_W2018

10 Aggregate functions summary
2/23/2019 Aggregate functions summary Can be used as part of an expression Use WHERE to limit the number of rows being aggregated Multiple aggregate functions can be used together Use AS to give an aggregate function an alias 3. Aggregate Calculations CS1222_W2018

11 Discussions SELECT avg(lengthseconds) FROM Tracks
2/23/2019 Discussions SELECT avg(lengthseconds) FROM Tracks Returns a SINGLE VALUE! SELECT lengthseconds Returns a TABLE! 3. Aggregate Calculations CS1222_W2018

12 Discussions SELECT tracktitle, avg(lengthseconds) FROM Tracks
2/23/2019 Discussions SELECT tracktitle, avg(lengthseconds) FROM Tracks Does this query make any sense? No, we need group by! 3. Aggregate Calculations CS1222_W2018

13 Outline Aggregate calculations for a table
2/23/2019 Outline Aggregate calculations for a table Aggregate calculations for groups 3. Aggregate Calculations CS1222_W2018

14 2/23/2019 What are groups? Member id Gender Region Country 11 M TX USA 12 ON CANADA 13 14 F VA 15 16 A group contains a collection of rows that have the same attribute(s) values. 3. Aggregate Calculations CS1222_W2018

15 What are groups? (cont.) Example groups All male members
2/23/2019 What are groups? (cont.) Example groups All male members Members from USA Members from Virginia female members from Ontario 3. Aggregate Calculations CS1222_W2018

16 2/23/2019 GROUP BY If SELECT has only aggregates, will report just one row for the entire set of records If non-aggregate fields are included in SELECT clause, query will report one row for each combination of non-aggregate field values with aggregates calculated for each Must include a GROUP BY clause listing the non-aggregate fields The more fields in the GROUP BY clause, the more rows will be reported Proper order of clauses: SELECT column, Aggregate(column) As column_name FROM tablename WHERE condition GROUP BY column ORDER BY column 3. Aggregate Calculations CS1222_W2018

17 Aggregate a single group
2/23/2019 Aggregate a single group Format: Select count|max|min|avg|sum From table_name Where conditions The conditions to form a group 3. Aggregate Calculations CS1222_W2018

18 Aggregate a single group example1:
2/23/2019 Aggregate a single group example1: Report the number of male members Select count(*) From Members Where gender = ‘m’ 3. Aggregate Calculations CS1222_W2018

19 Aggregate a single group example:
2/23/2019 Aggregate a single group example: Report the number of female members from Ontario Select count(*) From Members Where gender = ‘f’ and region = ‘on’ 3. Aggregate Calculations CS1222_W2018

20 Aggregate multiple groups
2/23/2019 Aggregate multiple groups How to answer the following queries using a single query? Report the number of male AND female members? For each distinct combination of gender and region, report the number of members Use group by!! 3. Aggregate Calculations CS1222_W2018

21 2/23/2019 GROUP BY When you include non-aggregate fields with aggregates, you must include a GROUP BY clause listing the non-aggregate fields. One row will be reported for each combination of the non-aggregate fields. SELECT column, Aggregate (column) FROM Tablename WHERE condition GROUP BY column ORDER BY column 3. Aggregate Calculations CS1222_W2018

22 What is a group? Report the number of male AND female members?
2/23/2019 What is a group? Report the number of male AND female members? SELECT gender, count(*) FROM Members GROUP BY gender Member id Gender Region Country 11 M TX USA 12 ON CANADA 13 14 F VA 15 16 Gender Count M F 3. Aggregate Calculations CS1222_W2018

23 GROUP BY Example 2/23/2019 Select Count(*) As Num From Members Where Is Not Null Num 12 Select Gender, Count(*) As Num From Members Where Is Not Null Group By Gender Gender Num F M 3. Aggregate Calculations CS1222_W2018

24 2/23/2019 What is a group? For each distinct combination of gender and region, report the number of members SELECT gender, region, count(*) FROM Members GROUP BY gender, region Member id Gender Region Country 11 M TX USA 12 ON CANADA 13 14 F VA 15 16 Gender region Count M TX M ON M VA F VA F ON 3. Aggregate Calculations CS1222_W2018

25 GROUP BY Example (cont)
2/23/2019 Select Region, Gender, Count(*) As Num From Members Where Is Not Null Group By Region, Gender Region Gender Num IN F VT F CA M GA M IN M NC M NY M OH M ONT M TX M VA M 3. Aggregate Calculations CS1222_W2018

26 GROUP BY Example Report the total time for EACH TITLE ID
2/23/2019 GROUP BY Example Report the total time for EACH TITLE ID SELECT titleId, sum(lengthseconds) FROM Tracks GROUP BY titleId 3. Aggregate Calculations CS1222_W2018

27 GROUP BY Example (cont.)
2/23/2019 GROUP BY Example (cont.) Report the shortest and longest track lengths for EACH TITLE ID SELECT titleId, Min(lengthseconds), Max(lengthseconds) FROM Tracks GROUP BY titleId 3. Aggregate Calculations CS1222_W2018

28 GROUP BY Example (cont)
2/23/2019 GROUP BY Example (cont) Report the number of members for each combination of country and gender SELECT country, gender, count(*) FROM Members GROUP BY country, gender 3. Aggregate Calculations CS1222_W2018

29 2/23/2019 GROUP BY summary Aggregate on distinct value(s) of the group-by attribute(s) Group-by attribute(s) must be the same as the non-aggregate attributes in the SELECT clause The more fields in the GROUP BY clause, the more rows will be reported 3. Aggregate Calculations CS1222_W2018

30 2/23/2019 HAVING HAVING restricts results based on aggregated values (eliminate groups) HAVING always uses an aggregate function as its test Proper order of clauses: SELECT column, Aggregate(column) FROM tablename WHERE condition GROUP BY column HAVING condition ORDER BY column 3. Aggregate Calculations CS1222_W2018

31 2/23/2019 HAVING vs. WHERE WHERE restricts results prior to aggregate calculations based on individual row values HAVING restricts results based on aggregated values HAVING always uses an aggregate function as its test Proper order of clauses: SELECT column, Aggregate(column) As column_name FROM tablename WHERE condition GROUP BY column HAVING condition ORDER BY column 3. Aggregate Calculations CS1222_W2018

32 Base selection with aggregate
2/23/2019 HAVING Example For each titleID, report the average length of all tracks. Select TitleID, Avg(lengthseconds) As AvgLength From Tracks Group by TitleID TitleID AvgLength Base selection with aggregate 3. Aggregate Calculations CS1222_W2018

33 2/23/2019 HAVING Example (cont) Select TitleID, Avg(lengthseconds) As AvgLength From Tracks Group by TitleID Having Avg(lengthseconds)>240 TitleID AvgLength HAVING eliminates aggregated averages of 240 seconds or less, so calculations match original but with fewer resulting rows 3. Aggregate Calculations CS1222_W2018

34 2/23/2019 HAVING Example (cont) Select TitleID, Avg(lengthseconds) As AvgLength From Tracks Where lengthseconds>240 Group by TitleID TitleID AvgLength WHERE eliminates detail records with length of 240 seconds or less, so reported averages are higher and no groups are eliminated. 3. Aggregate Calculations CS1222_W2018

35 2/23/2019 HAVING Example (cont) Select TitleID, Avg(lengthseconds) As AvgLength From Tracks Where lengthseconds>400 Group by TitleID TitleID AvgLength WHERE eliminates detail records with length of 400 seconds or less, so reported averages are higher. 3. Aggregate Calculations CS1222_W2018

36 Summary Aggregate calculations for a table
2/23/2019 Summary Aggregate calculations for a table Sum, max, min, avg, count Aggregate calculations for groups Group by having 3. Aggregate Calculations CS1222_W2018


Download ppt "CS122 Using Relational Databases and SQL"

Similar presentations


Ads by Google