TRANSFORM … PIVOT
Summarises rows as a grid: one row per group, and one column per value of a pivot expression. Access calls this a crosstab query.
Access SQL. ACE accepts this statement as written here. LibRed names, orders and fills the columns as ACE does.
Syntax
Section titled “Syntax”TRANSFORM aggregate [ [ AS ] alias ]SELECT [ ALL | DISTINCT | DISTINCTROW ] row_heading [ , … ] FROM table_source [ , … ] [ IN external_database ] [ WHERE condition ] GROUP BY expression [ , … ][ ORDER BY expression [ ASC | DESC ] [ , … ] ]PIVOT pivot_expression [ IN ( heading [ , … ] ) ][ WITH OWNERACCESS OPTION ]Arguments
Section titled “Arguments”aggregate
Section titled “aggregate”The value in each cell: an aggregate over the rows of that cell’s group and pivot value, such as
Count(OrderID) or Sum(Freight). It may be an expression over an aggregate, such as Sum(V) * 2. A value
that is not an aggregate is refused: “Your query does not include the TRANSFORM value as part of an aggregate
function.”
row_heading
Section titled “row_heading”The SELECT list: the columns that come first in each row, before the pivot columns. Name one with AS, as in
a SELECT. A row heading may itself be an aggregate, which gives a row total over the whole group, whatever the
pivot columns are: SELECT ShipCountry, Sum(Freight) AS Total.
FROM, WHERE
Section titled “FROM, WHERE”The rows to summarise, as in SELECT.
GROUP BY expression [ , … ]
Section titled “GROUP BY expression [ , … ]”Required. Each group is one row of the result. Every GROUP BY key makes its own rows, whether or not it is in
the SELECT list.
ORDER BY
Section titled “ORDER BY”Orders the rows. It comes before PIVOT. It may not use an aggregate.
PIVOT pivot_expression
Section titled “PIVOT pivot_expression”The expression whose values become the columns. Each distinct value is one column, after the row headings.
IN ( heading [ , … ] )
Section titled “IN ( heading [ , … ] )”Fixed column headings. The pivot columns are then exactly these, in this order: a heading the data lacks is
still a column (all Null), and a value no heading names gives no column. A heading matches the values that =
calls equal to it, so '9' matches the number 9, and 'A' matches 'a'. Each heading is a literal, and may
appear once.
WITH OWNERACCESS OPTION
Section titled “WITH OWNERACCESS OPTION”Comes after the PIVOT. LibRed has no users to act for, so it changes nothing; see SELECT.
Remarks
Section titled “Remarks”Column names and order
Section titled “Column names and order”Without fixed headings, the pivot columns are in ascending order of value:
- Text in the database’s sort order, with upper and lower case taken as the same value.
- Numbers in numeric order.
- A Boolean as its number:
-1(True), then0(False). A comparison such asFreight > 100pivots this way. - Null comes first, in a column named
<>. An empty text value is also named<>.
A column is named after its value, made into a valid name: a . becomes _ (1.5 is the column 1_5), as do
!, [ and ], and leading and trailing spaces are dropped. A fixed heading is named as it is written in the
IN list (True, not -1).
A cell holds the aggregate over the rows of its group and pivot value, and Null where there are no such rows.
When the query returns no rows, the result has only the row headings and any fixed headings as columns.
Stored crosstabs
Section titled “Stored crosstabs”A crosstab can be stored with CREATE PROCEDURE, and run by name with
EXECUTE or used as a table in another query’s FROM clause. Declared parameters bind as in a
SELECT; see PARAMETERS.
When a crosstab’s columns are described without running it (CommandBehavior.SchemaOnly), only the row
headings and fixed headings are reported, because the other columns come from the data.
Refused
Section titled “Refused”These are refused, as ACE refuses them:
| Statement uses | Error |
|---|---|
HAVING |
Syntax error in TRANSFORM statement. |
no GROUP BY |
Syntax error in TRANSFORM statement. |
SELECT * |
Cannot use ‘*’ in crosstab query. |
TOP |
Values specified in a TOP clause are not allowed in delete queries or reports. |
| a fixed heading twice | Cannot specify fixed column heading ‘…’ in a crosstab query more than once. |
a heading that is an expression, such as 9+1 |
Data type mismatch in criteria expression. |
Examples
Section titled “Examples”These run against the Northwind sample database, as in the playground.
Orders per employee, one column per shipper:
TRANSFORM Count(OrderID)SELECT EmployeeID FROM OrdersGROUP BY EmployeeIDPIVOT ShipViaThe result has the columns EmployeeID, 1, 2 and 3.
Freight per country and year, with a row total and the rows in descending order:
TRANSFORM Sum(Freight)SELECT ShipCountry, Sum(Freight) AS TotalFROM OrdersWHERE ShipCountry IN ('France', 'Germany', 'Spain')GROUP BY ShipCountryORDER BY ShipCountry DESCPIVOT Year(OrderDate)The columns are ShipCountry, Total, 1996, 1997 and 1998.
Fixed headings, in the order given:
TRANSFORM Count(OrderID)SELECT EmployeeID FROM OrdersGROUP BY EmployeeIDPIVOT Format(OrderDate, 'mmm') IN ('Jan', 'Feb', 'Mar', 'Dec')A heading the data lacks is still a column. No order here was shipped to Finland, so Finland is all Null:
TRANSFORM Count(OrderID)SELECT ShipVia FROM OrdersWHERE ShipCountry IN ('Norway', 'Poland')GROUP BY ShipViaPIVOT ShipCountry IN ('Norway', 'Poland', 'Finland')Pivot on a comparison. The columns are -1 (more than 100) and 0:
TRANSFORM Sum(Freight)SELECT ShipCountry FROM OrdersWHERE ShipCountry IN ('France', 'Germany')GROUP BY ShipCountryPIVOT Freight > 100Norway’s orders have no ship region, so their one column is <>:
TRANSFORM Count(OrderID)SELECT ShipVia FROM OrdersWHERE ShipCountry = 'Norway'GROUP BY ShipViaPIVOT ShipRegionOrders per employee and quarter, for a year given as a parameter:
PARAMETERS [Which year] SHORT;TRANSFORM Count(OrderID)SELECT EmployeeID FROM OrdersWHERE Year(OrderDate) = [Which year]GROUP BY EmployeeIDPIVOT DatePart('q', OrderDate)Store a crosstab, then query it as a table:
CREATE PROCEDURE ShipperCounts ASTRANSFORM Count(OrderID) SELECT EmployeeID FROM Orders GROUP BY EmployeeID PIVOT ShipViaSELECT EmployeeID, [1] FROM ShipperCounts WHERE EmployeeID < 4