27
Database Systems dr Grzegorz Michalski 14 May 2014 Database Systems 1/26

Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Database Systems

dr Grzegorz Michalski

14 May 2014

Database Systems 1/26

Page 2: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

General Functions

The following instructions work with any data type and pertain tousing nulls:

NVL (expr1, expr2)

NVL2 (expr1, expr2, expr3)

NULLIF(expr1, expr2)

COALESCE (expr1, expr2, . . . , exrn)

Database Systems 2/26

Page 3: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

NVL Function

Converts a null value to an actual value:

Data types that can be used are date, character, and number.

Data types must match:

NVL(commission pct,0)

NVL(hire date,’01=JAN-97’)

NVL(job id, ’No Job Yet’)

Database Systems 3/26

Page 4: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

NVL, NVL2 examples

Example

SELECT last name, salary, NVL(commission pct, 0),

(salary*12 + (12*salary*NVL(commission pct, 0))

ANNUAL SALLARY,

FROM employees;

Example

SELECT last name, salary, commission pct,

NVL2(commission pct, ’SAL and COMM’,’SAL’) income

FROM employees

WHERE department id IN (40,90);

Database Systems 4/26

Page 5: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

NVL, NVL2 examples

Example

SELECT last name, salary, NVL(commission pct, 0),

(salary*12 + (12*salary*NVL(commission pct, 0))

ANNUAL SALLARY,

FROM employees;

Example

SELECT last name, salary, commission pct,

NVL2(commission pct, ’SAL and COMM’,’SAL’) income

FROM employees

WHERE department id IN (40,90);

Database Systems 4/26

Page 6: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

NULLIF example

Example

SELECT first name, LENGTH(first name) "expr1",

last name, LENGTH(last name) "expr2"

NULLIF(LENGTH(first name), LENGTH(first name)) res

FROM employees;

Database Systems 5/26

Page 7: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Usign the COALESCE Function

1 The adventage of the COALESCE function over the NVL

function is that the COALESCE function can take multiplealternate values.

2 If the first expression is not null, the COALESCE functionreturns that expression; otherwise, it does a COALESCE of theremaining expressions.

Example

SELECT last name, employee id,

COALESCE(TO CHAR(commission pct),

TO CHAR(manager id),

’No commision and no manager’) Info

FROM employees;

Database Systems 6/26

Page 8: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

COALESCE exmaple

Example

SELECT last name, salary, commission pct,

COALESCE((salary + (commission pct*salary)),

salary+2000, salary) ”New Salary”)FROM employees;

Database Systems 7/26

Page 9: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Conditional Expressions

1 Provide the use of the IF--THEN--ELSE logic within a SQLstatement.

2 Use a two methods:

CASE expression.DECODE function.

CASE expression

Facilitates conditional inquiries by doing the work of anIF--THEN--ELSE:CASE expr WHEN comparison expr1 THEN return expr1

[WHEN comparison expr2 THEN return expr2

WHEN comparison exprn THEN return exprn

ELSE else expr]

END

Database Systems 8/26

Page 10: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the CASE expression

Example

SELECT last name, job id, salary,

CASE job id WHEN ’IT PROG’ THEN 2.50*salary

WHEN ’ST CLERK’ THEN 1.20*salary

WHEN ’SA REP’ THEN 1.25*salary

ELSE salary

END "Revised salary"

FROM employees;

Example

SELECT last name, job id, salary,

CASE WHEN salary < 5000 THEN ’Low’

WHEN salary < 10000 THEN ’Medium’

WHEN salary < 15000 THEN ’High’

ELSE ’Excellent’

END Salary

FROM employees;

Database Systems 9/26

Page 11: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

DECODE Function

Facilitates conditional inquiries by doing work of a CASE expressionor an IF--THEN--ELSE statementDECODE(col|expression, search1, result1

[, search2, result2,. . . ][, default])

Example

SELECT last name, job id, salary,

DECODE(job id, ’IT PROG’, 2.50*salary,

’ST CLERK’, 1.20*salary,

’SA REP’, 1.25*salary,

salary)

"Revised salary"

FROM employees;

Database Systems 10/26

Page 12: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

DECODE example

Example

SELECT last name, salary,

DECODE( TRUNC( SALARY/2000, 0),

0, 0.00,

1, 0.09,

2, 0.20,

3, 0.30,

4, 0.40,

5, 0.42,

5, 0.44,

0.45)

"Tax Rate"

FROM employees;

Database Systems 11/26

Page 13: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Reporting Aggregated Data

Using the Group Functions

Database Systems 12/26

Page 14: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

What are group Functions?

group functions operate on sets of rows to give one result pergroup.

Types of group functions

AVG

COUNT

MAX

MIN

STDDEV

VARIANCE

Database Systems 13/26

Page 15: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Types of group functions

Function Description

AVG([DISTINCT|ALL] n) Averange value of n,ignoring nulls

COUNT([DISTINCT|ALL] expr) Number of rows, where exprevaluates to somethingother than nulls

MAX([DISTINCT|ALL] expr) Maximum value of exprignoring nulls

MIN([DISTINCT|ALL] expr) Minimum value of expr,ignoring nulls

STDDEV([DISTINCT|ALL] n) Standard deviation of n,ignoring nulls

SUM([DISTINCT|ALL] n) Sum values of n, ignoring nulls

VARIANCE([DISTINCT|ALL] n) Variance of n, ignoring nulls

Database Systems 14/26

Page 16: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Group Functions: Syntax

SELECT group function(column), . . .FROM table

[WHERE conditions];

Using the AVG and SUM Functions

You may use AVG and SUM for numeric data.

Example

SELECT AVG(salary), MAX(salary),

MIN(salary), SUM(salary)

FROM employees

WHERE job id LIKE ‘%REP%’;

Database Systems 15/26

Page 17: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the MIN and MAX Functions

You can use the MIN and MAX functions for numeric, character, anddate data types.

Example

SELECT MIN(hire date), MAX(hire date),

FROM employees

Example

SELECT MIN(last name), MAX(last name),

FROM employees

Database Systems 16/26

Page 18: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the COUNT Function

Example

COUNT(*) returns the number of rows in a table:SELECT COUNT(*)

FROM employees

WHERE department id = 50

Example

COUNT(expr) returns the number of rows with non–null values forexpr:SELECT COUNT(commission pct)

FROM employees

WHERE department id = 50

Database Systems 17/26

Page 19: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the DISTINCT Keyword

COUNT(DISTINCT expr) retuns the number of distinctnon–null values of expr

To display the number of distinct department values in theEMPLOYEES table:

Example

SELECT COUNT(DISTINCT department id)

FROM employees;

Database Systems 18/26

Page 20: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Group Functions and Null Values

All group functions ignore null values in column,

The NVL function forces group functions to include null values.

Example

SELECT AVG(commision pct), AVG(NVL(commision pct, 0))

FROM employees;

Database Systems 19/26

Page 21: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Creating Groups of Data: GROUP BY Clause Syntax

You can divide rows in a table into smaller groups by using theGROUP BY clause:SELECT column, gropu function(column)

FROM table

[WHERE condition]

[GROUP BY group by expression]

[ORDER BY column]

Database Systems 20/26

Page 22: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the GROUP BY Clause

All the columns in the SELECT list that are not in group functionsmust be in the GROUP BY clause.

Example

SELECT department id, AVG (salary)

FROM employees

GROUP BY department id;

Database Systems 21/26

Page 23: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the GROUP BY Clause

The GROUP BY column does not have to be in the SELECT list.

In the ORDER BY clause you may use a group function.

Example

SELECT AVG(salary)

FROM employees

GROUP BY department id;

Example

SELECT department id, AVG(salary)

FROM employees

GROUP BY department id

ORDER BY AVG(salary)

Database Systems 22/26

Page 24: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the GROUP BY Clause on Multiple Columns

Example

SELECT department id, job id, SUM(salary)

FROM emplyees

WHERE department id > 20

GROUP BY department id, job id

ORDER BY department id;

Database Systems 23/26

Page 25: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Illegal Queries Using Group Functions

Any column or expression in the SELECT list that is not anaggregate function must be in the GROUP BY clause.

If you use a aggregate function, you must use a GROUP BY

clause.

Example

SELECT department id, COUNT(last name)

FROM employees;

Example

SELECT department id, job id, COUNT(last name)

FROM employees

GROUP BY department id;

Database Systems 24/26

Page 26: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Restricting Group Results with the HAVING Clause

When you use the HAVING clause, the Oracle server restrictsgroups as follows:

Rows are grouped.

The group function is applied.

Groups matching the HAVING clause are displayed.

SELECT column, gropu function(column)

FROM table

[WHERE condition]

[GROUP BY group by expression]

[HAVING group condition]

[ORDER BY column]

Database Systems 25/26

Page 27: Database Systems - icis.pcz.plicis.pcz.pl/~michalski/download/erasmus/w06.pdfReporting Aggregated Data Using the Group Functions Database Systems 12/26. What are group Functions? group

Using the HAVING clause

Example

SELECT department id, MAX(salary)

FROM employees

GROUP BY department id

HAVING MAX(salary) > 10000;

Example

SELECT job id, SUM(salary) payroll

FROM employees

WHERE job id NOT LIKE ’%REP%’

GROUP BY job id

HAVING SUM(salary) > 12500

ORDER BY SUM(salary);

Database Systems 26/26