If you just need the definition of a view for a single query, you can use a table expression.
Table expressions are temporary and are only valid for the life of the SQL statement; they cannot be shared, but they allow more flexibility than views. View definitions can be shared by any authorized user.
This section describes how to use common table expressions and nested table expressions in queries.
A nested table expression is a temporary view where the definition is nested (defined directly) in the FROM clause of the main query.
The following query uses a nested table expression to find the average total pay, education level and year of hire, for those with an education level greater than 16:
SELECT EDLEVEL, HIREYEAR, DECIMAL(AVG(TOTAL_PAY), 7,2) FROM (SELECT YEAR(HIREDATE) AS HIREYEAR, EDLEVEL, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE WHERE EDLEVEL > 16 ) AS PAY_LEVEL GROUP BY EDLEVEL, HIREYEAR ORDER BY EDLEVEL, HIREYEAR
The result is as follows:
EDLEVEL HIREYEAR 3 ------- ----------- --------- 17 1967 28850.00 17 1973 23547.00 17 1977 24430.00 17 1979 25896.50 18 1965 57970.00 18 1968 32827.00 18 1973 45350.00 18 1976 31294.00 19 1958 51120.00 20 1975 42110.00
This query uses a nested table expression to first extract the year of hire from the HIREDATE column so that it can subsequently be used in the GROUP BY clause. You may not want to create this as a view, because you intend to perform similar queries using different values for EDLEVEL.
The scalar built-in function DECIMAL is used in this example. DECIMAL returns a decimal representation of a number or a character string. For more details on functions refer to the SQL Reference.
A common table expression is a named result table that is defined using the WITH keyword prior to the beginning of a fullselect. It is a table expression that you create to use throughout a complex query. Define and name it at the start of the query using a WITH clause. Repeated references to a common table expression use the same result set. By comparison, if you used nested table expressions or views, the result set would be regenerated each time, with possibly different results.
The following example lists all the people in the company who have an education level greater than 16, who make less pay on average than those people who were hired at the same time and who have the same education. The parts of the query are described in further detail following the query.
(1)
WITH PAYLEVEL AS (SELECT EMPNO, YEAR(HIREDATE) AS HIREYEAR, EDLEVEL, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE WHERE EDLEVEL > 16),
(2)
PAYBYED (EDUC_LEVEL, YEAR_OF_HIRE, AVG_TOTAL_PAY) AS (SELECT EDLEVEL, HIREYEAR, AVG(TOTAL_PAY) FROM PAYLEVEL GROUP BY EDLEVEL, HIREYEAR)
(3)
SELECT EMPNO, EDLEVEL, YEAR_OF_HIRE, TOTAL_PAY, DECIMAL(AVG_TOTAL_PAY,7,2) FROM PAYLEVEL, PAYBYED WHERE EDLEVEL=EDUC_LEVEL AND HIREYEAR = YEAR_OF_HIRE AND TOTAL_PAY < AVG_TOTAL_PAY
The final result is as follows:
EMPNO EDLEVEL YEAR_OF_HIRE TOTAL_PAY 5 ------ ------- ------------ ------------- --------- 000210 17 1979 20132.00 25896.50