Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / SQL reference.pdf
Скачиваний:
24
Добавлен:
12.05.2015
Размер:
11.92 Mб
Скачать

LAST_DAY

Analytic Example

The next example makes the same calculation as the previous example but returns the result for each employee within the department:

SELECT last_name, department_id, salary,

MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)

OVER (PARTITION BY department_id) "Worst",

MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)

OVER (PARTITION BY department_id) "Best"

FROM employees

ORDER BY department_id, salary;

LAST_NAME

DEPARTMENT_ID

SALARY

Worst

Best

-------------------

------------- ---------- ---------- ----------

Whalen

10

4400

4400

4400

Fay

20

6000

6000

13000

Hartstein

20

13000

6000

13000

.

 

 

 

 

.

 

 

 

 

.

 

 

 

 

Gietz

110

8300

8300

12000

Higgins

110

12000

8300

12000

Grant

 

7000

7000

7000

LAST_DAY

Syntax last_day::=

LAST_DAY ( date )

Purpose

LAST_DAY returns the date of the last day of the month that contains date.

Examples

The following statement determines how many days are left in the current month.

SELECT SYSDATE,

LAST_DAY(SYSDATE) "Last",

LAST_DAY(SYSDATE) - SYSDATE "Days Left"

FROM DUAL;

Functions 6-83

LAST_VALUE

SYSDATE Last

Days Left

--------- --------- ----------

30-MAY-01 31-MAY-01

1

The following example adds 5 months to the hire date of each employee to give an evaluation date:

SELECT last_name, hire_date, TO_CHAR(

ADD_MONTHS(LAST_DAY(hire_date), 5)) "Eval Date"

FROM employees;

LAST_NAME

HIRE_DATE Eval Date

-------------------------

--------- ---------

King

17-JUN-87 30-NOV-87

Kochhar

21-SEP-89 28-FEB-90

De Haan

13-JAN-93 30-JUN-93

Hunold

03-JAN-90 30-JUN-90

Ernst

21-MAY-91 31-OCT-91

Austin

25-JUN-97 30-NOV-97

Pataballa

05-FEB-98 31-JUL-98

Lorentz

07-FEB-99 31-JUL-99

.

 

.

 

.

 

LAST_VALUE

Syntax last_value::=

LAST_VALUE ( expr ) OVER ( analytic_clause )

See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions

Purpose

LAST_VALUE is an analytic function. It returns the last value in an ordered set of values.

You cannot use LAST_VALUE or any other analytic function for expr. That is, you can use other built-in function expressions for expr, but you cannot nest analytic functions.

6-84 Oracle9i SQL Reference

LAST_VALUE

See Also: "About SQL Expressions" on page 4-2 for information on valid forms of expr

Examples

The following example returns, for each row, the hire date of the employee earning the highest salary.

SELECT last_name, salary, hire_date, LAST_VALUE(hire_date) OVER (ORDER BY salary

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lv FROM (SELECT * FROM employees WHERE department_id = 90

ORDER BY hire_date);

LAST_NAME

SALARY

HIRE_DATE LV

-------------------------

----------

--------- ---------

Kochhar

17000

21-SEP-89 17-JUN-87

De Haan

17000

13-JAN-93 17-JUN-87

King

24000

17-JUN-87 17-JUN-87

This example illustrates the nondeterministic nature of the LAST_VALUE function. Kochhar and De Haan have the same salary, so they are in adjacent rows. Kochhar appears first because the rows in the subquery are ordered by hire_date.

However, if the rows are ordered by hire_date in descending order, as in the next example, then the function returns a different value:

SELECT last_name, salary, hire_date, LAST_VALUE(hire_date) OVER (ORDER BY salary

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lv FROM (SELECT * FROM employees WHERE department_id = 90

ORDER BY hire_date DESC);

LAST_NAME

SALARY

HIRE_DATE LV

-------------------------

----------

--------- ---------

De Haan

17000

13-JAN-93 17-JUN-87

Kochhar

17000

21-SEP-89 17-JUN-87

King

24000

17-JUN-87 17-JUN-87

The following two examples show how to make the LAST_VALUE function deterministic by ordering on a unique key. By ordering within the function by both salary and hire_date, you can ensure the same result regardless of the ordering in the subquery.

Functions 6-85

LEAD

SELECT last_name, salary, hire_date, LAST_VALUE(hire_date) OVER (ORDER BY salary, hire_date

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lv FROM (SELECT * FROM employees WHERE department_id = 90

ORDER BY hire_date);

 

LAST_NAME

SALARY HIRE_DATE LV

-------------------------

---------- --------- ---------

Kochhar

17000 21-SEP-89 17-JUN-87

De Haan

17000 13-JAN-93 17-JUN-87

King

24000 17-JUN-87 17-JUN-87

SELECT last_name, salary, hire_date, LAST_VALUE(hire_date) OVER (ORDER BY salary, hire_date

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lv FROM (SELECT * FROM employees WHERE department_id = 90

ORDER BY hire_date DESC);

LAST_NAME

SALARY

HIRE_DATE LV

-------------------------

----------

--------- ---------

Kochhar

17000

21-SEP-89 17-JUN-87

De Haan

17000

13-JAN-93 17-JUN-87

King

24000

17-JUN-87 17-JUN-87

LEAD

Syntax lead::=

 

 

,

offset

,

default

 

LEAD

(

value_expr

 

 

 

)

 

 

query_partition_clause

 

 

 

OVER

 

(

 

order_by_clause

)

See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions

Purpose

LEAD is an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and

6-86 Oracle9i SQL Reference

Соседние файлы в папке Oracle selected docs