Skip to content

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.

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 ]

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.”

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.

The rows to summarise, as in SELECT.

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.

Orders the rows. It comes before PIVOT. It may not use an aggregate.

The expression whose values become the columns. Each distinct value is one column, after the row headings.

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.

Comes after the PIVOT. LibRed has no users to act for, so it changes nothing; see SELECT.

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), then 0 (False). A comparison such as Freight > 100 pivots 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.

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.

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.

These run against the Northwind sample database, as in the playground.

Orders per employee, one column per shipper:

TRANSFORM Count(OrderID)
SELECT EmployeeID FROM Orders
GROUP BY EmployeeID
PIVOT ShipVia

The 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 Total
FROM Orders
WHERE ShipCountry IN ('France', 'Germany', 'Spain')
GROUP BY ShipCountry
ORDER BY ShipCountry DESC
PIVOT Year(OrderDate)

The columns are ShipCountry, Total, 1996, 1997 and 1998.

Fixed headings, in the order given:

TRANSFORM Count(OrderID)
SELECT EmployeeID FROM Orders
GROUP BY EmployeeID
PIVOT 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 Orders
WHERE ShipCountry IN ('Norway', 'Poland')
GROUP BY ShipVia
PIVOT ShipCountry IN ('Norway', 'Poland', 'Finland')

Pivot on a comparison. The columns are -1 (more than 100) and 0:

TRANSFORM Sum(Freight)
SELECT ShipCountry FROM Orders
WHERE ShipCountry IN ('France', 'Germany')
GROUP BY ShipCountry
PIVOT Freight > 100

Norway’s orders have no ship region, so their one column is <>:

TRANSFORM Count(OrderID)
SELECT ShipVia FROM Orders
WHERE ShipCountry = 'Norway'
GROUP BY ShipVia
PIVOT ShipRegion

Orders per employee and quarter, for a year given as a parameter:

PARAMETERS [Which year] SHORT;
TRANSFORM Count(OrderID)
SELECT EmployeeID FROM Orders
WHERE Year(OrderDate) = [Which year]
GROUP BY EmployeeID
PIVOT DatePart('q', OrderDate)

Store a crosstab, then query it as a table:

CREATE PROCEDURE ShipperCounts AS
TRANSFORM Count(OrderID) SELECT EmployeeID FROM Orders GROUP BY EmployeeID PIVOT ShipVia
SELECT EmployeeID, [1] FROM ShipperCounts WHERE EmployeeID < 4