Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.

Similar presentations


Presentation on theme: "Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co."— Presentation transcript:

1 Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.

2 2/30 Contents Chapter 11 Functions and Groups 11.1 Using Functions in Queries 11.1 A. String Functions 11.1 B. Numeric Functions 11.1 C. Date Functions 11.1 D. Logical Functions 11.2 Aggregate Functions 11.2 A. The Function COUNT() 11.2 B. The Function SUM() 11.2 C. The Function AVG() 11.2 D. The Function MAX() 11.2 E. The Function MIN() 11.3 Group 11.3 A. Group and Aggregate Functions11.3 A. Group and Aggregate Functions 11.3 B. Grouping by Multiple Columns11.3 B. Grouping by Multiple Columns 11.3 C. Applying Filtering before Grouping11.3 C. Applying Filtering before Grouping 11.3 D. The HAVING clause 11.3 E. Combing HAVING and WHERE11.3 E. Combing HAVING and WHERE 11.3 F. Grouping by Non-column Names11.3 F. Grouping by Non-column Names 11.3 G. Using GROUP BY with ORDER BY11.3 G. Using GROUP BY with ORDER BY

3 Copyright 2005 Radian Publishing Co.3/30 Chapter 11 Functions and Groups We shall make use of the functions available in your DBMS to give more varied results from a database. A function accepts one or more input data through the arguments and returns a result (see textbook Appendix D).

4 Copyright 2005 Radian Publishing Co.4/30 11.1 Using Functions in Queries The built-in functions available from DBMS are categorised into four main classes: String Numeric Date Logical (see Appendix D on the Textbook for more information)

5 Copyright 2005 Radian Publishing Co.5/30 11.1 A. String Functions Common string functions include: LENGTH(String) UPPER(String), LOWER(String) LTRIM(String), RTRIM(String), TRIM(String), LEFT(String, n), RIGHT(String, n), MID(String, n, m), INSTR(String, SubStr) ASCII(String) CHAR(String) CONCAT(String1, String2,...), etc.

6 Copyright 2005 Radian Publishing Co.6/30 11.1 B. Numeric Functions Common numeric functions include: ABS(X) SQRT(X) ROUND(X,D), TRUNCATE(X, 0) MOD(N,M) POW(X,Y) RAND() FORMAT(X, D), etc.

7 Copyright 2005 Radian Publishing Co.7/30 11.1 C. Date Functions Common date functions include: CURDATE() DAY(Date), MONTH(Date), YEAR(Date) DAYOFWEEK(Date) DATE_ADD(Date) DAYNAME(Date), MONTHNAME(Date), etc. MS Access does not allow reserved words, like DAY, MONTH and YEAR, as column aliases.

8 Copyright 2005 Radian Publishing Co.8/30 11.1 D. Logical Functions (1/3) The logical function is: IF(expr1, expr2, expr3). If expr1 is TRUE then IF() returns expr2, else it returns expr3.

9 Copyright 2005 Radian Publishing Co.9/30 11.1 D. Logical Functions (2/3)

10 Copyright 2005 Radian Publishing Co.10/30 11.1 D. Logical Functions (3/3) Note MS Access does not accept alias in the ORDER BY clause. Therefore, the expression IIF(…) has to be repeated. For Visual FoxPro, alias must be used in the ORDER BY clause

11 Copyright 2005 Radian Publishing Co.11/30 11.2 Aggregate Functions Aggregate functions return a value based on multiple values in a column. Examples are: COUNT() SUM() AVG() MAX() MIN()

12 Copyright 2005 Radian Publishing Co.12/30 11.2 A. The Function COUNT() (1/2) COUNT() returns the number of rows retrieved by a SELECT statement. Usually, it is used as COUNT(*). It does not make sense to mix an aggregate function with unrelated fields.

13 Copyright 2005 Radian Publishing Co.13/30 11.2 A. The Function COUNT() (2/2)

14 Copyright 2005 Radian Publishing Co.14/30 11.2 B. The Function SUM() SUM() adds the specified fields in a column and returns the sum.

15 Copyright 2005 Radian Publishing Co.15/30 11.2 C. The Function AVG() AVG() function computes the average of a column.

16 Copyright 2005 Radian Publishing Co.16/30 11.2 D. The Function MAX() MAX() returns the largest value in a column. The MAX() function can also work on dates and strings.

17 Copyright 2005 Radian Publishing Co.17/30 11.2 E. The Function MIN() MIN() works returns the lowest number of a column.

18 Copyright 2005 Radian Publishing Co.18/30 11.3 Group (1/2) Records which have the same value in a column can form a group. The syntax of SELECT is now extended to: SELECT [DISTINCT] SelectItems FROM TableName WHERE Conditions GROUP BY GroupColumn1 [,GroupColumn2 ] ORDER BY OrderItem1 [, OrderItem2... ;

19 Copyright 2005 Radian Publishing Co.19/30 11.3 Group (2/2)

20 Copyright 2005 Radian Publishing Co.20/30 11.3 A. Group and Aggregate Functions The power of GROUP BY is evident when it is used with aggregate functions. The aggregate function returns a value for each group.

21 Copyright 2005 Radian Publishing Co.21/30 11.3 B. Grouping by Multiple Columns Similar to ORDER BY, you may put more than one field in the GROUP BY clause.

22 Copyright 2005 Radian Publishing Co.22/30 11.3 C. Applying Filtering before Grouping Filtering of records is done by the WHERE clause.

23 Copyright 2005 Radian Publishing Co.23/30 11.3 D. The HAVING clause (1/2) HAVING clause is designed to work with aggregate functions or group. The HAVING clause must be placed after GROUP BY and before ORDER BY.

24 Copyright 2005 Radian Publishing Co.24/30 11.3 D. The HAVING clause (2/2)

25 Copyright 2005 Radian Publishing Co.25/30 11.3 E. Combing HAVING and WHERE (1/2) Don't use HAVING for items that should be in the WHERE clause. WHERE clause filters records and HAVING clause filters groups. In combing the two, the WHERE clause is placed before the HAVING clause and will be performed first.

26 Copyright 2005 Radian Publishing Co.26/30 11.3 E. Combing HAVING and WHERE (2/2)

27 Copyright 2005 Radian Publishing Co.27/30 11.3 F. Grouping by Non-column Names (1/2) Expressions can be used in the GROUP BY clause.

28 Copyright 2005 Radian Publishing Co.28/30 11.3 F. Grouping by Non-column Names (2/2)

29 Copyright 2005 Radian Publishing Co.29/30 11.3 G. Using GROUP BY with ORDER BY (1/2) The order of execution is: WHERE, GROUP BY, HAVING, ORDER BY.

30 Copyright 2005 Radian Publishing Co.30/30 11.3 G. Using GROUP BY with ORDER BY (2/2)


Download ppt "Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co."

Similar presentations


Ads by Google