, a leading Access applications company, which designs custom solutions to improve operations for small and large organizations in all sectors.)<\/em><\/p>\nA well designed database stores data in a normalized format with dates defined in a field so that new data is simply added as additional records. However, people want to see data with dates grouped by columns.<\/p>\n
This can be done by using a crosstab query. However, when creating reports based on crosstab queries, we need to control the specific column names that are returned by the query. Otherwise, the report cannot refer to the query’s fields.<\/p>\n
As an example, we will use a slightly modified version of the Northwind 2007 Template. The following crosstab summarizes the sales for previous year, with a column for each month:<\/p>\n
<\/p>\n
<\/p>\n
The query sql looks like this:<\/p>\n
TRANSFORM Sum(CCur([Quantity]*[Unit Price]*(1-[Discount]))) AS Price<\/p>\n
SELECT [Order Details].[Product ID]<\/p>\n
FROM [Order Summary] INNER JOIN [Order Details] ON [Order Summary].[Order ID] = [Order Details].[Order ID]<\/p>\n
WHERE (((Format([Order Date],”yyyy”))=Format(Date(),”yyyy”)-1))<\/p>\n
GROUP BY [Order Details].[Product ID]<\/p>\n
ORDER BY Format([Order Date],”mmm”)<\/p>\n
PIVOT Format([Order Date],”mm”);<\/p>\n
This works great until you run into a month that doesn’t have any sales. For example, assume that you have no sales in July:<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
Notice that in addition to being sorted undesirably, there is no column for July. This poses a problem, for example, if you want to use the crosstab query as the datasource for a report, Access 2010 reports reference field names directly. If you want to use a crosstab query as the RecordSource of a report, its column names should be static.<\/p>\n
Use the PIVOT IN Clause to Specify Required Column Names<\/h3>\n In Design view<\/strong>, open the Property Sheet<\/strong> for the query, and set the Column Headings<\/strong> property to show the desired columns in the desired order:<\/p>\n <\/p>\n
<\/p>\n
<\/p>\n
The query sql looks like this:<\/p>\n
TRANSFORM Sum(CCur([Quantity]*[Unit Price]*(1-[Discount]))) AS Price<\/p>\n
SELECT [Order Details].[Product ID]<\/p>\n
FROM [Order Summary] INNER JOIN [Order Details] ON [Order Summary].[Order ID] = [Order Details].[Order ID]<\/p>\n
WHERE (((Format([Order Date],”yyyy”))=Format(Date(),”yyyy”)-1))<\/p>\n
GROUP BY [Order Details].[Product ID]<\/p>\n
ORDER BY Format([Order Date],”mmm”)<\/p>\n
PIVOT Format([Order Date],”mmm”) IN (“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”);<\/p>\n
Now that you have static column names, you can use the query to drive a report, referring to the known query names:<\/p>\n
<\/p>\n
For more information about using crosstab queries on reports, check out our tip here http:\/\/www.fmsinc.com\/MicrosoftAccess\/query\/crosstab-report\/index.html.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"A well designed database stores data in a normalized format with dates defined in a field so that new data is simply added as additional records. However, people want to see data with dates grouped by columns. This can be done by using a crosstab query. However, when creating reports based on crosstab queries, we need to control the specific column names that are returned by the query. Otherwise, the report cannot refer to the query’s fields.<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_classifai_error":"","footnotes":""},"content-type":[4035],"product":[4037,4036],"audience":[4081,4102],"tags":[],"coauthors":[4437],"class_list":["post-1812","post","type-post","status-publish","format-standard","hentry","content-type-tips-and-guides","product-access","product-microsoft-365","audience-enterprise","audience-small-business"],"yoast_head":"\n
Using crosstab queries in reports | Microsoft 365 Blog<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n