Derived tables allow you to nest queries within a FROM clause. With derived tables, you can perform grouping of groups, or you can construct a join with a group, without having to create a separate view or table and join to it.
In the following example, the inner SELECT statement (enclosed in parentheses) creates a derived table, grouped by customer ID values. The outer SELECT statement assigns this table the correlation name sales_order_counts and joins it to the Customers table using a join condition.
SELECT Surname, GivenName, number_of_orders FROM Customers JOIN ( SELECT CustomerID, COUNT(*) FROM SalesOrders GROUP BY CustomerID ) AS sales_order_counts ( CustomerID, number_of_orders ) ON ( Customers.ID = sales_order_counts.CustomerID ) WHERE number_of_orders > 3;
The result is a table of the names of those customers who have placed more than three orders, including the number of orders each has placed.
Discuter à propos de cette page dans DocCommentXchange.
|Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1|