10.12.2012 Views

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

Using common table expressions with CREATE VIEW<br />

You can use common table expressions before a fullselect in a CREATE VIEW<br />

statement. The common table expression must be placed immediately inside of the<br />

statement. This is useful if you need to use the results of a common table<br />

expression in more than one query.<br />

Example: Using a WITH clause in a CREATE VIEW statement: The following<br />

statement finds the departments that have a greater than average total pay <strong>and</strong><br />

saves the results as the view RICH_DEPT:<br />

CREATE VIEW RICH_DEPT (deptno) AS<br />

WITH DTOTAL (deptno, totalpay) AS<br />

(SELECT deptno, sum(salary+bonus)<br />

FROM DSN8810.EMP<br />

GROUP BY deptno)<br />

SELECT deptno<br />

FROM DTOTAL<br />

WHERE totalpay > (SELECT AVG(totalpay)<br />

FROM DTOTAL);<br />

The fullselect in the previous example uses the result table for DTOTAL to find the<br />

departments that have a greater than average total pay. The result table is saved as<br />

the RICH_DEPT view <strong>and</strong> looks similar to the following results:<br />

DEPTNO<br />

======<br />

A00<br />

D11<br />

D21<br />

Using common table expressions when you use INSERT<br />

You can use common table expressions before a fullselect in an INSERT statement.<br />

The common table expression must be placed immediately inside of the statement.<br />

Example: Using a WITH clause in an INSERT statement: The following example<br />

illustrates the use of a common table expression in an INSERT statement.<br />

INSERT INTO vital_mgr (mgrno) AS<br />

WITH VITALDEPT (deptno, se_count) AS<br />

(SELECT deptno, count(*)<br />

FROM DSN8810.EMP<br />

WHERE job = ’senior engineer’<br />

GROUP BY deptno)<br />

SELECT d.manager<br />

FROM DSN8810.DEPT d, VITALDEPT s<br />

WHERE d.deptno = s.deptno<br />

AND s.se_count > (SELECT AVG(se_count)<br />

FROM VITALDEPT);<br />

The fullselect in the previous example uses the result table for VITALDEPT to find<br />

the manager’s number for departments that have a greater than average number of<br />

senior engineers. The manager’s number is then inserted into the vital_mgr table.<br />

Using recursive <strong>SQL</strong><br />

You can use common table expressions to create recursive <strong>SQL</strong>. If a fullselect of a<br />

common table expression contains a reference to itself in a FROM clause, the<br />

common table expression is a recursive common table expression. Queries that use<br />

recursion are useful in applications like bill of materials applications, network<br />

planning applications, <strong>and</strong> reservation systems.<br />

Recursive common table expressions must follow these rules:<br />

Chapter 1. Retrieving data 15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!