Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
all labs.pdf
Скачиваний:
74
Добавлен:
25.02.2016
Размер:
2.04 Mб
Скачать

Using the DISTINCT Keyword

COUNT(DISTINCT expr) returns the number of distinct non-null values of expr.

To display the number of distinct department values in the

EMPLOYEES table:

SELECT COUNT(DISTINCT department_id)

FROM employees;

 

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

Using the DISTINCT Keyword

 

 

 

 

Oracle

 

Use the DISTINCT keyword to suppress the counting of any dup icate values in a column.

 

The example in the slide displays the number of distinct dep rtment values that are in the

 

EMPLOYEES table.

 

Only

 

 

 

&

 

 

Internal

 

 

Oracle

Use

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 10

Group Functions and Null Values
Copyright © 2009, Oracle. All rights reserved.

Group Functions and Null Values

Group functions ignore null values in the column:

1 SELECT AVG(commission_pct)

FROM employees;

The NVL function forces group functions to include null values:

 

 

 

 

2

SELECT

AVG(NVL(commission_pct, 0))

 

FROM

employees;

 

 

 

 

However, the NVL function forces group functions to include null values.

Academy All group functions ignore null values in the column.Oracle

Examples:

1. The average is calculated based on only those Onlyrows in the table in which a valid value is stored in the COMMISSION_PCT column. &The average is calcu ated as the total commission that is

paid to all employees divided by the number of employees receiving a commission (four).

2. The average isInternalcalculated based on all rows in the table, regardless of whether null values are stored in the COMMISSION PCTUsecolumn. The average is calculated as the total commission

that is paid to all employ s divided by the total number of employees in the company (20). Oracle

Oracle Database 11g: SQL Fundamentals I 5 - 11

Lesson Agenda

Group functions:

Types and syntax

Use AVG, SUM, MIN, MAX, COUNT

Use DISTINCT keyword within group functions

NULL values in a group function

Grouping rows:

GROUP BY clause

HAVING clause

Nesting group functions

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

Oracle

&

Only

 

 

Internal

 

 

 

 

Use

 

Oracle

 

 

 

Oracle Database 11g: SQL Fundamentals I 5 - 12

Creating Groups of Data

 

EMPLOYEES

 

 

 

 

 

 

 

 

4400

Average salary in

 

 

 

 

 

 

 

 

EMPLOYEES table for

 

 

 

 

9500

 

 

 

 

 

 

 

 

each department

 

 

 

 

 

 

 

 

 

3500

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6400

 

 

 

 

 

 

10033

 

 

 

 

 

 

 

 

 

 

 

 

 

Academy

 

 

Copyright © 2009, Oracle. All rights reserved.

Creating Groups of Data

 

 

 

 

 

 

 

 

 

Oracle

Until this point in our discussion, all group functions have treat d the table as one large group of information. At times, however, you need to divide the table of information into smaller groups. This can be done by using the GROUP BY clause.

&

Only

Internal

 

Use

Oracle

 

Oracle Database 11g: SQL Fundamentals I 5 - 13

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]