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

EXTRACT (XML)

SELECT EXTRACT(TIMEZONE_REGION

FROM TIMESTAMP ’1999-01-01 10:00:00 -08:00’)

FROM DUAL;

EXTRACT(TIMEZONE_REGIONFROMTIMESTAMP’1999-01-0110:00:00-08:00’)

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

UNKNOWN

The ambiguity arises because the time zone numerical offset is provided in the expression, and that numerical offset may map to more than one time zone region.

EXTRACT (XML)

Syntax extract_xml::=

, namespace_string

EXTRACT ( XMLType_instance , XPath_string

Purpose

EXTRACT (XML) is similar to the EXISTSNODE function. It applies a VARCHAR2 XPath string and returns an XMLType instance containing an XML fragment. The optional namespace_string must resolve to a VARCHAR2 value that specifies a default mapping or namespace mapping for prefixes, which Oracle uses when evaluating the XPath expression(s).

Examples

The following example extracts the value of the /Warehouse/Dock node of the XML path of the warehouse_spec column in the sample table oe.warehouses:

SELECT warehouse_name, EXTRACT(warehouse_spec, ’/Warehouse/Docks’) "Number of Docks"

FROM warehouses

WHERE warehouse_spec IS NOT NULL;

WAREHOUSE_NAME

Number of Docks

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

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

Southlake, Texas

<Docks>2</Docks>

San Francisco

<Docks>1</Docks>

New Jersey

<Docks/>

Seattle, Washington

<Docks>3</Docks>

Functions 6-65

EXTRACTVALUE

Compare this example with the example for EXTRACTVALUE on page 6-66, which returns the scalar value of the XML fragment.

EXTRACTVALUE

Syntax extractvalue::=

 

 

 

 

,

namespace_string

EXTRACTVALUE

(

XMLType_instance

,

XPath_string

)

The EXTRACTVALUE function takes as arguments an XMLType instance and an XPath expression and returns a scalar value of the resultant node. The result must be a single node and be either a text node, attribute, or element. If the result is an element, the element must have a single text node as its child, and it is this value that the function returns. If the specified XPath points to a node with more than one child, or if the node pointed to has a non-text node child, Oracle returns an error. The optional namespace_string must resolve to a VARCHAR2 value that specifies a default mapping or namespace mapping for prefixes, which Oracle uses when evaluating the XPath expression(s).

For documents based on XML schemas, if Oracle can infer the type of the return value, then a scalar value of the appropriate type is returned. Otherwise, the result is of type VARCHAR2. For documents that are not based on XML schemas, the return type is always VARCHAR2.

Examples

The following example takes as input the same arguments as the example for EXTRACT (XML) on page 6-65. Instead of returning an XML fragment, as does the EXTRACT function, it returns the scalar value of the XML fragment:

SELECT warehouse_name,

EXTRACTVALUE(e.warehouse_spec, ’/Warehouse/Docks’) "Docks"

FROM warehouses e

WHERE warehouse_spec IS NOT NULL;

6-66 Oracle9i SQL Reference

FIRST

WAREHOUSE_NAME

Docks

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

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

Southlake, Texas

2

San Francisco

1

New Jersey

 

Seattle, Washington

3

FIRST

Syntax

first::=

aggregate_function

KEEP

 

 

 

 

 

 

 

 

 

 

 

,

 

 

 

 

 

 

DESC

FIRST

 

 

 

 

 

 

 

NULLS

 

 

 

 

 

 

ASC

LAST

(

DENSE_RANK

FIRST

ORDER

BY

expr

)

 

OVER

query_partitioning_clause

 

 

 

See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions of the ORDER BY clause and OVER clause

Purpose

FIRST and LAST are very similar functions. Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST with respect to a given sorting specification. If only one row ranks as FIRST or LAST, the aggregate operates on the set with only one element.

When you need a value from the first or last row of a sorted group, but the needed value is not the sort key, the FIRST and LAST functions eliminate the need for self joins or views and enable better performance.

The aggregate_function is any one of the MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV functions. It operates on values from the rows that rank either FIRST or LAST. If only one row ranks as FIRST or LAST, the aggregate operates on a singleton (nonaggregate) set.

Functions 6-67

FIRST

DENSE_RANK FIRST or DENSE_RANK LAST indicates that Oracle will aggregate over only those rows with the minimum (FIRST) or the maximum (LAST) dense rank ("olympic rank").

You can use the FIRST and LAST functions as analytic functions by specifying the OVER clause. The query_partitioning_clause is the only part of the OVER clause valid with these functions.

Aggregate Example

The following example returns, within each department of the sample table hr.employees, the minimum salary among the employees who make the lowest commission and the maximum salary among the employees who make the highest commission:

SELECT department_id,

MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct) "Worst", MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct) "Best"

FROM employees

GROUP BY department_id;

DEPARTMENT_ID

Worst

Best

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

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

10

4400

4400

20

6000

13000

30

2500

11000

40

6500

6500

50

2100

8200

60

4200

9000

70

10000

10000

80

6100

14000

90

17000

24000

100

6900

12000

110

8300

12000

 

7000

7000

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"

6-68 Oracle9i SQL Reference

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