In addition to using aggregate functions to aggregate data, you can create custom expressions to produce aggregate values. You can use custom expressions in place of aggregate functions anywhere in an aggregate query.
For example, in the titles
table you might want to create a query that shows not just the average price, but what the average price would be if it were discounted.
You cannot include an expression that is based on calculations involving only individual rows in the table; the expression must be based on an aggregate value, because only the aggregate values are available at the time the expression is calculated.
How?
Background information
If you want to create subtotals or show other summary information for subsets of a table, you create groups using an aggregate query. Each group summarizes the data for all the rows in the table that have the same value.
For example, you might want to see the average price of a book in the titles
table, but break the results down by publisher. To do so, you group the query by publisher (for example, pub_id
). The resulting query output might look like this:
When you group data, you can display only summary or grouped data, such as:
pub_id
is the grouped column. price
column. You cannot display values from individual rows. For example, if you group only by publisher, you cannot also display individual titles in the query. Therefore, if you add columns to the query output, the Query Designer automatically adds them to the GROUP BY clause of the statement in the SQL pane. If you want a column to be aggregated instead, you can specify an aggregate function for that column.
If you group by more than one column, each group in the query shows the aggregate values for all grouping columns.
For example, the following query against the titles
table groups by publisher (pub_id
) and also by book type (type
). The query results are ordered by publisher and show summary information for each different type of book that the publisher produces:
SELECT pub_id, type, SUM(price) Total_price
FROM titles
GROUP BY pub_id, type
The resulting output might look like this:
Group rows
How?
When you create a query, you are retrieving data from a table, view, or function. To work with any of these objects in your query, you add them to the Diagram pane.
Add a table, view, or user-defined function to the query
The Query Designer updates the Diagram pane, Grid pane, and SQL pane accordingly.
Alternatively, you can drag objects onto the Diagram pane. You can drag a table, view, or inline function from the database window.
You can also drag columns or tables from the Database Designer or paste them from the Clipboard.
Tables and views are automatically added to the query when you reference them in the statement in the SQL pane.
The Query Designer will not display data columns for an table, view, or inline function if you do not have sufficient access rights. In such cases, only a title bar and the * (All Columns) check box are displayed for the table, view, or inline function.
Add an existing query to a new query
For more information on SQL aliases and subqueries, see the Microsoft SQL Server documentation.
The Query Designer adds a GROUP BY clause to the statement in the SQL pane. For example, the SQL statement might look like this:
SELECT pub_id
FROM titles
GROUP BY pub_id
The Query Designer automatically assigns a column alias to the column you are summarizing. You can replace this automatically generated alias with a more meaningful one.
The corresponding statement in the SQL pane might look like this:
SELECT pub_id, SUM(price) AS Totalprice
FROM titles
GROUP BY pub_id
The Query Designer automatically assigns a column alias to the expression to create a useful column heading in query output.