The Query Builder allows you to use a Table Expression in your query.
You will use a Table Expression when your data is to come from an SQL statement, rather than a table. Table Expressions are also known as Inline Views or sub-queries.
An example of a query which uses a Table Expression is
SELECT * FROM
Customers a,
(SELECT CustomerID, count(*) as num FROM Orders GROUP BY CustomerID) b
WHERE a.CustomerID=b.CustomerID
There are two “tables” in this query:
Customers
(SELECT CustomerID, count(*) AS num FROM Orders GROUP BY CustomerID)
This second of these is a Table Expression. Most databases support the use of Table Expressions, which provide a mechanism for developing powerful queries.
To specify a new Table Expression, click on New Expression. Enter the SQL text for the table expression.
The table will now appear in the query with the name (expression). If you create more than one Table Expression, the table-ids will differentiate them.
You can amend the table expression by clicking on the Edit Expression button.
You can create a Table Expression in another Query Builder window. If you click on New Expression (in QB), AQT will open a new Query Builder window in which you can create a query to be used as a Table Expression.
Clicking on Edit Expression in QB will allow you to edit a Table Expression in a Query Builder window
A Table Expression must generally have a Table-Id. AQT will normally generate one unless you have set the option not to use Table-Id (Options > Table ID > no table id), or if it is the only table in the query.
Any generated or summary columns in your Table Expression should be given names with the AS clause.
Invalid |
no proper name for second column, difficult to reference |
|
Valid |
|
|
In order to determine the columns in the Table Expression, AQT does a “prepare” on the SQL for the Table Expression. Some databases do not support a standalone prepare, and may run the SQL; this might cause a delay when you enter or change a Table Expression.