Creating a Pivot Table or Pivot Chart from a Query
Pivot tables and pivot charts provide great ways for you to summarize detailed data stored in your Jet and SQL Server databases. Pivot tables present your data in a spreadsheet-like format, whereas pivot charts automatically render pivot table views as line, bar, or area charts. New in Access 2002 were two new views for queries: PivotTable and PivotChart. These views are available for both your conventional Jet applications (MDB) and your Access Projects (ADP).
Creating the Query to Display in PivotTable or PivotChart View
The first thing you must do is build a query that is appropriate to display in PivotTable or PivotChart view. Queries that lend themselves to be displayed in pivot table or pivot chart view provide many ways for the user to manipulate their data. An example of such a query is one that contains information about country, city, salesperson, sales, and date of sale. You could determine sales by city and salesperson for each month, or you could determine sales in each country for each salesperson during the year 2002. As you can see, the idea of pivot tables is to let you slice and dice the data in any way you need to at a given moment in time.For our example, create the following query within the Northwind database:
SalesPerson:Employees!LastName & ", " & Employees!FirstName .
Total:[Order Details]!UnitPrice * [Order Details]!Quantity .
We now have a query on which we can base our pivot table.
Displaying the Query in PivotTable View
To switch to PivotTable view, select the PivotTable View button on the toolbar. The empty PivotTable view appears and the Access toolbar changes to the PivotTable toolbar (see Figure 4.50).
Figure 4.50. When you switch to PivotTable view, the empty PivotTable view appears and the Access toolbar changes to the PivotTable toolbar.

The PivotTable Field List window also appears. There are four types of fields that you will add to your pivot table. They include the following:
- Column fields
Often hold date fields; generally hold information with the fewest number of data items. - Row fields
One or more fields that display data by attributes. - Totals or Detail fields
The crosstab data itself. These are the numeric values that make up the meat of the pivot table. - Filter fields
One or more optional fields that restrict that data appearing in the columns, rows, or both.
To display our initial pivot table, take the following steps:
Figure 4.51. The pivot table that appears after dragging and dropping the Total so that it appears as a Detail field.

Displaying Summarized Data
Including all the detail data may be much more detail than you need. You can alter the query design to show only summary information. Here's how it works:
Figure 4.52. The query that appears after changing the Total cell for the data field to Sum.


Figure 4.53. The underlying query after modifying it to summarize the data.

Figure 4.54. PivotTable view after modifying the underlying query to summarize the data.

Filtering Pivot Table Data
By default, Access includes all data in the pivot table. You can filter the pivot table to display only selected values for a row or column. For example, you can filter to display data for only sales in specific countries. Here's how it works:
Figure 4.55. The list contains an item for each field value.

Using Drill-Down
Generally the initial pivot table contains an excessive amount of detail. Here's how you can modify the amount of detail to show summary information only:
Exchanging Axes
If you are viewing Year across the top and Country down the side, and you decide to view Year across the side and Country across the top, simply drag and drop their name buttons to switch the positions in which they appear. You can also easily drag new items to the pivot table from the PivotTable list at any time, or remove them from the pivot table entirely.
Switching to PivotChart View
When you define a PivotTable view, you automatically generate a PivotChart view. You will see how evident this is by using the View tool to switch to PivotChart view. The results appear in Figure 4.56. You can use toolbar buttons to add legends and to modify the chart type.
Figure 4.56. PivotChart view shows the pivot table as a chart.
