A single WITH clause may define more than one common table expression. These definitions must be separated by commas. The following example lists the department that has the smallest payroll and the department that has the largest number of employees.
WITH CountEmployees( DepartmentID, n ) AS ( SELECT DepartmentID, COUNT( * ) AS n FROM Employees GROUP BY DepartmentID ), DepartmentPayroll( DepartmentID, amount ) AS ( SELECT DepartmentID, SUM( Salary ) AS amount FROM Employees GROUP BY DepartmentID ) SELECT count.DepartmentID, count.n, pay.amount FROM CountEmployees AS count JOIN DepartmentPayroll AS pay ON count.DepartmentID = pay.DepartmentID WHERE count.n = ( SELECT MAX( n ) FROM CountEmployees ) OR pay.amount = ( SELECT MIN( amount ) FROM DepartmentPayroll ); |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |