Common table expressions are defined using the WITH clause, which precedes the SELECT keyword in a SELECT statement. The content of the clause defines one or more temporary views that are known only within the scope of a single SELECT statement and that may be referenced elsewhere in the statement. The syntax of this clause mimics that of the CREATE VIEW statement.
Common table expressions are useful and may be necessary if a query involves multiple aggregate functions or defines a view within a stored procedure that references program variables. Common table expressions also provide a convenient means to temporarily store sets of values.
For example, consider the problem of determining which department has the most employees. The Employees table in the SQL Anywhere sample database lists all the employees in a fictional company and specifies in which department each works. The following query lists the department ID codes and the total number of employees in each department.
SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID; |
This query can be used to extract the department with the most employees as follows:
SELECT DepartmentID, n FROM ( SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID ) AS a WHERE a.n = ( SELECT MAX( n ) FROM ( SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID ) AS b ); |
While this statement provides the correct result, it has some disadvantages. The first disadvantage is that the repeated subquery makes this statement less efficient. The second is that this statement provides no clear link between the subqueries.
One way around these problems is to create a view, then use it to re-express the query. This approach avoids the problems mentioned above.
CREATE VIEW CountEmployees( DepartmentID, n ) AS SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID; SELECT DepartmentID, n FROM CountEmployees WHERE n = ( SELECT MAX( n ) FROM CountEmployees ); |
The disadvantage of this approach is that some overhead is required, as the database server must update the system tables when creating the view. If the view will be used frequently, this approach is reasonable. However, when the view is used only once within a particular SELECT statement, the preferred method is to instead use a common table expression as follows.
WITH CountEmployees( DepartmentID, n ) AS ( SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID ) SELECT DepartmentID, n FROM CountEmployees WHERE n = ( SELECT MAX( n ) FROM CountEmployees ); |
Changing the query to search for the department with the fewest employees demonstrates that such queries may return multiple rows.
WITH CountEmployees( DepartmentID, n ) AS ( SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID ) SELECT DepartmentID, n FROM CountEmployees WHERE n = ( SELECT MIN( n ) FROM CountEmployees ); |
In the SQL Anywhere sample database, two departments share the minimum number of employees, which is 9.
Multiple correlation names
Multiple table expressions
Where common table expressions are permitted
Typical applications of common table expressions
Recursive common table expressions
Parts explosion problem
Least distance problem
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |