Microsoft access 2007 crosstab


















This should give you a reasonable idea of how crosstab queries work, but if you're still a little confused, don't fret. An example will clear things up. For this example, we're going to look at is a report regarding company sales transactions. First, we'll need to create a table to hold our sales.

We'll call it SalesTransaction. It's a relatively simple table that contains the basic key elements most sales tables have: a type of product, the sale price of the product, and the date of the sales transaction. Figure A shows the design of the table in Microsoft Access. You can download a sample database containing this table if you want to follow along with this example. I created dummy data for the SalesTransactions table so that we can produce some queries from it.

The query named qryCreateSampleData in the sample database will introduce you to this useful tool for creating dummy data for testing purposes. Now that we have a structure and table to query, we're ready to create our crosstab query.

Open a new query window and add the SalesTransactions table to the query designer. You'll notice that when you select this query option, the aggregations are instantly flipped on.

Our goal is to present to management a report detailing product sales by quarter. Of course, we could present this information through a typical SELECT query; however, the data would be much more difficult to absorb and would probably require some massaging by management to present the numbers in a more meaningful way.

This is what's great about crosstab queries: They enable you to present data in a way that's easier to read and understand. Because we're going to look at product sales by quarter, we need to aggregate data by our ProductType and our TransactionDate columns.

How you display the data depends on your preference, but when I'm running crosstab queries based upon quarters, months, or years, I typically like to present date data as column headers, which will leave me to display the product data as a row heading.

Once we've set our Row Heading and Column Heading options, we need to decide which Value crosstab option to use. We'll use the ProductPrice field here because we want to see sales information, which means we'll need to SUM that data. You can use only one field each when you specify column headings and values to summarize.

You can use as many as three fields when you specify row headings. You can also use an expression to produce row headings, column headings, or values to summarize. One, two, or three columns on this side contain row headings. The names of the fields that you use as row headings appear in the top row of these columns. The row headings appear here. The number of rows in the crosstab datasheet can grow quickly when you use more than one row heading field, because each combination of row headings is displayed.

The columns on this side contain column headings and summary values. Note that the name of the column heading field does not appear on the datasheet. It does most of the work for you, but there are a few options that the wizard does not offer. It is easy to use. To use it, you start the wizard, and then answer a series of guided questions. It can automatically group dates into intervals.

Instead, create the crosstab query in Design view, and use an expression to create the intervals. It can be used as a starting point. You can use the wizard to create the basic crosstab query that you want, and then fine-tune the query's design by using Design view. At the last step of the wizard, you can choose to modify the query in Design view. This allows you to add query design elements that the wizard does not support, such as additional record sources.

Work in Design view Design view allows you more control over your query design. It supports the features that are not available in the wizard. However, you cannot specify parameter data types by using SQL view. If you want to use a parameter in your crosstab query, you must specify the parameter data type by modifying your query in Design view. Tip: Remember — you are not limited to using only one method for creating a crosstab query.

You can use the wizard to create the query, and then use Design view to modify the query design. Using the Crosstab Query Wizard requires that you use a single table or query as the record source for your crosstab query. If a single table does not have all the data that you want to include in your crosstab query, start by creating a select query that returns the data that you want.

For more information about creating a select query, refer to the See Also section. For this example, we'll create a crosstab query using the Products table from the Northwind sample database. We want to display a count of the number of products in each category from all the suppliers. On the Create tab, in the Queries group, click Query Wizard. On the first page of the wizard, choose the table or query that you want to use to create a crosstab query.

For this example, select the Products table and then click Next. On the next page, choose the field that contains the values that you want to use as row headings. You can select up to three fields to use as row headings sources, but the fewer row headings you use, the easier your crosstab datasheet will be to read.

If you choose more than one field to supply row headings, the order in which you choose the fields determines the default order in which your results are sorted. For this example, select Supplier IDs. Notice that Access displays the field name along the left side of the sample query preview at the bottom of the dialog box. Click Next to continue. On the next page, choose the field that contains the values that you want to use as column headings.

In general, you should choose a field that contains few values, to help keep your results easy to read. For example, using a field that has only a few possible values such as gender might be preferable to using a field that can contain many different values such as age.

For this example, select Category and notice that Access displays category sample names along the top of the sample query preview at the bottom of the dialog box. On the next page, choose a field and a function to use to calculate summary values. The data type of the field that you select determines which functions are available.

On the same page, select or clear the Yes, include row sums check box to include or exclude row sums. If you include row sums, the crosstab query has an additional row heading that uses the same field and function as the field value.

Including a row sum inserts an additional column that summarizes the remaining columns. For example, if your crosstab query calculates average age by location and gender with gender column headings , the additional column calculates the average age by location, across all genders. For this example, select ID in the Fields box and Count in the Functions box in order to have Access count the number of products in each intersection of supplier and category.

Leave the Yes, include row sums check box selected. Access will create a column that totals the number of products from each supplier. On the last page of the wizard, type a name for your query and then specify whether you want to view the results or modify the query design.

You can change the function that is used to produce row sums by editing the crosstab query in Design view. If you've walked through this example using the Products table from the Northwind database, the crosstab query displays the list of supplier names as rows, the product category names as columns, and a count of the number of products in each intersection.

By using Design view to create your crosstab query, you can use as many record sources tables and queries as you want. However, you can keep the design simple by first creating a select query that returns all of the data that you want and then using that query as the only record source for your crosstab query. When you build a crosstab query in Design view, you use the Total and Crosstab rows in the design grid to specify which field's values will become column headings, which fields' values will become row headings, and which field's values to sum, average, count, or otherwise calculate.

The settings in these rows determine whether the field is a row heading, column heading, or summary value. On the Create tab, in the Queries group, click Query Design. In the Show Table dialog box, double-click each table or query that you want to use as a record source.

If you use more than one record source, make sure that the tables or queries are joined on fields that they have in common. For more information about joining tables and queries, refer to the See Also section.

On the Design tab, in the Query Type group, click Crosstab. In the query design window, double-click each field that you want to use as a source of row headings. You can select as many as three fields for row headings. In the query design grid, in the Crosstab row for each row heading field, select Row Heading.

You can enter an expression in the Criteria row to limit the results for that field. You can also use the Sort row to specify a sort order for a field.

You can select as many as three fields for row headings. In the query design grid, in the Crosstab row for each row heading field, select Row Heading. You can enter an expression in the Criteria row to limit the results for that field. You can also use the Sort row to specify a sort order for a field.

In the query design window, double-click the field that you want to use as the source of column headings. You can select only one field for column headings. In the query design grid, in the Crosstab row for the column heading field, select Column Heading.

You can enter an expression in the Criteria row to limit the results for the column heading field. However, using a criteria expression with the column heading field does not limit the number of columns returned by the crosstab query. Instead, it limits which columns contain data. For example, suppose you have a column heading field that has three possible values: red, green, and blue. If you want to limit the values that display as column headings, you can specify a list of fixed values by using the query's Column Headings property.

For more information, see the next section. In the query design window, double-click the field that you want to use to calculate summary values. You can select only one field to use for summary values.

In the query design grid, in the Total row for the summary values field, select an aggregate function to use to calculate the values. In the Crosstab row for the summary values field, select Value. On the Design tab, in the Results group, click Run.

If you want to specify fixed values to use for column headings, you can set the query's Column Headings property. In the property sheet, just above the General tab, make sure that the Selection type is Query Properties. If it is not, click an empty spot in the space above the query design grid.

In the property sheet, on the General tab, in the Column Headings property, enter a comma-separated list of values that you want to use as column headings. Some characters such as most punctuation marks are not allowed in column headings. SQL view does not limit the number of tables or queries that you can use as record sources for a crosstab query.

However, you can help keep the design simple by creating a select query that returns all of the data that you want to use in your crosstab query, and then using that select query as the record source. On the Create tab, in the Other group, click Query Design. If you are using more than one table or query as a record source, include the table or query name as part of each field name; for example, Sum [Expense].

Separate the list items by using commas; for example, [Budget]. On the third line, after FROM , type a list of the tables or queries that you are using as record sources; for example, Budget, Expense.

If you want to sort in descending order, type DESC after the field name or expression. If you want to sort on an additional field or expression, type a comma and then type the additional field name or expression. For example, IN , , , produces four column headings: , , , If you specify a fixed value that does not correspond to a field value from the pivot field, that fixed value becomes a column heading for an empty column.

You can also use parentheses to group criteria into logical sets. Sometimes, rather than using every value of a field for row or column headings, you want to group the values of a field into ranges and then use those ranges for row or column headings. For example, suppose you use an "Age" field for column headings.

Rather than using a column for each age, you may prefer to use columns that represent age ranges. You can use the IIf Function in an expression to create ranges to use for row or column headings. If none of these intervals is what you want, you should create your crosstab query in Design view, and then use the technique described in this section to create the intervals that you want. In the query design grid, in the Field row, right-click an empty column, and then click Zoom on the shortcut menu.

In the Zoom box, type a field alias, followed by a colon :. Inside the parentheses that follow IIf , type a comparison expression that defines the first range of field values. For example, suppose you are creating ranges for an Age field, and you want each range to be twenty years. Type a comma after the comparison expression, and then type a name for the range, enclosed in quotation marks.

The name you supply is the crosstab heading for values that fall within the range. Type a comma after the range name outside the quotation marks , and then do one of the following:. For example, a complete nested IIf expression that separates an Age field into twenty-year ranges might look like this line breaks were added for readability :.

Note: When Access evaluates the expression, it stops evaluating as soon as one of the IIf statements evaluates to true. You do not need to specify the lower end of each range, because any value that falls below the lower end of a given range would already have evaluated as true.

In the query design grid, in the Total row, select Group By. In the Crosstab row, specify whether to use the ranges as row headings or column headings. Remember that you can specify from one to three row headings, and one column heading.

You may want your crosstab query to prompt for input when it is run. Rather than always displaying data for every country or region, you may want your query to prompt for a name and then display data according to the user's input. Note: You can also add a parameter prompt to a column heading field, but this will not limit the columns that appear.

In the Criteria row for the row heading field for which you want to prompt the user for input, type question text enclosed in square brackets. The question text will appear as a prompt when the query is run.

For example, if you type [Which country or region? Tip: If you want your parameter to be flexible, concatenate your expression with wildcards, by using the Like operator. For example, instead of using [Which country or region?

Using Like does not change the appearance of the parameter prompt. In the Query Parameters dialog box, in the Parameters column, enter the same parameter prompt that you used in the Criteria row.

Include the square brackets, but do not include any concatenated wildcards or the Like operator. In the Data Type column, select the data type for the parameter. The data type should match the data type of the row heading field.

If the field that you use to calculate summary values in your crosstab query contains null values, those values are ignored by any aggregate function that you use. For some aggregate functions, your results can be affected. For example, to calculate an average, you add all of the values and divide the result by the number of values. But if the field contains any null values, those null values are not counted as part of the number of values. In some cases, you may prefer to replace any null values with zeroes so that the values will be counted during aggregate calculations.



0コメント

  • 1000 / 1000