Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Summary Query.

Similar presentations


Presentation on theme: "Chapter 4 Summary Query."— Presentation transcript:

1 Chapter 4 Summary Query

2 บทที่ 4 การประมวลผลฐานข้อมูลเชิงธุรกิจ (Summary Query)
ฝึกปฏิบัติการการประมวลผลฐานข้อมูลเชิงธุรกิจ (SQL, Join, and Summary Query)

3 Agenda How to code summary queries
1. How to work with aggregate functions 2. How to group and summarize data

4 1. How to work with aggregate functions

5 Aggregate Functions Sometimes referred as column functions and summary queries. Aggregate functions operate on the values in columns.

6 Syntax of the Aggregate Functions

7 Syntax of the Aggregate Functions
Aggregate functions called column functions, perform a calculation on the values in a set of selected rows SELECT statement includes one or more aggregate functions AVG and SUM functions must result in a numeric value MIN, MAX, and COUNT functions can result in numeric, date, or string value Use DISTINCT keyword to omit duplicate values (typically used with the COUNT function) All of the aggregate functions except for COUNT (*) ignore null values The SELECT statement includes a GROUP BY clause, the SELECT clause can include the columns user for grouping, aggregate functions, and expressions that result in a constant value

8 A Summary Query that counts unpaid invoice and calculates the total due
Calculate selected values in a set of selected rows

9 A Summary Query that use the COUNT(*), AVG, and SUM functions

10 A Summary Query that use the MIN and MAX functions

11 A Summary Query that Works on non-numeric columns

12 A Summary Query that uses the DISTINCT keyword
To count all of the selected rows Use COUNT (*) function Use COUNT function with the name of any column that can’t contain null values Use DISTINCT keyword to count only the rows with unique values in a specified column

13 2. How to group and summarize data

14 Group and summarize data
Using GROUP BY and HAVING clauses

15 Syntax of the SELECT statement with GROUP BY and HAVING clauses

16 A summary Query that calculates the average invoice amount by vendor

17 A summary query that counts the number of invoices by vendor

18 A summary query that calculates the number of invoices and the average invoice amount for the vendors in each state and city

19 A summary query that limits the groups to those with two or more invoices

20 How the HAVING clause compares with the WHERE clause
Can contain aggregate function Can’t contain aggregate function Can refer to columns included in the SELECT clause Can refer to any columns in the base tables

21 A summary query with a search condition in the HAVING clause

22 A summary query with a search condition in the WHERE clause

23 How to code compound search conditions

24 A summary query with compound condition in the HAVING clause

25 The same query code with a WHERE clause

26 How to use the WITH ROLLUP operator
WITH ROLLUP operator can add one or more summary rows to the result set that uses grouping and aggregates

27 A summary query that includes a final summary row
Group by a single column Group the invoices by vendor_id and calculates an invoice count and invoice total for each vendor group This query adds a summary row to the end of the result set

28 A summary query that includes a summary row for each grouping level
Group by two columns This query groups vendors by state and city and counts the number of vendors in each group This query adds a summary row to the end state and adds a final summary row at the end of result set

29 WITH ROLLUP The WITH ROLLUP operator adds a summary row for each group specified in the GROUP BY clause. It also adds a summary row to the end of the result set that summarizes the entire result set. If the GROUP BY clause specifies a single group, the WITH ROLLUP operator only adds the final summary row When you use the WITH ROLLUP operator, you can’t use the ORDER BY clause. You can code the ASC or DESC keyword after the column in the GROUP BY clause to sort individual columns When you use the WITH ROLLUP operator, you can’t use the DISTINCT keyword in any of the aggregate functions.

30 Questions

31 References Murach, J. (2012). Murach’s SQL. Mike Murach & Associates, Inc. 205.pdf ork/My%20Guitar%20Shop%20Chapter%205.pdf


Download ppt "Chapter 4 Summary Query."

Similar presentations


Ads by Google