MS Access - Grouping Data


Advertisements


In this chapter, we will be covering how to calculate on a group of records in Access. We have created a field that calculates row by row or record by record to create a line total or subtotal field, but what if we wanted to calculate down by a group of records rather than by individual ones. We can do this by creating what's known as an Aggregate Query.

Aggregate Query

An aggregate query also known as a totals or summary query is a sum, mass or group particulars. It can be a total or gross amount or a group or subset of records. Aggregate queries can perform a number of operations. Here's a simple table listing some of the ways to total on a group of records.

S.No Aggregate Functions & Description
1.

Sum

Adds the field values

2.

Avg

Average of the field values

3.

Min

Lowest (minimum) field value

4.

Max

Highest (maximum) field value

5.

Count

Count of the values (records)

6.

StDev

Standard deviation of the field values including date/time fields

7.

Var

Variance of the field values including date/time

Let us open your database and go to the Query Design and include the following tables −

TblBooks

Close this dialog box and the following two fields will be displayed as shown in the query grid in the following screenshot.

Screenshot

This is a simple query and we are displaying only two fields — book title and quantity and when we run it we're seeing every single individual order in our database.

Let us now run this query and you will see the following results.

Individual Order

This is why book titles are repeating. Separate orders have been placed for each of these books here and they were ordered in different quantities. Let us assume we want to see a summary of only that book title that has been listed only once. And then the sum of the quantity listed beside it.

Let us now go to the Design View and in the Design tab, you will see a Sigma symbol. This is your totals button.

Screenshot

Click the sigma symbol which will open another row underneath your field in the table row and from here, you can specify how you are grouping this query.

GroupBy

We will now group by book title and also sum our quantity field.

If we click on group by area and further click on the drop-down menu, all the options will be listed down. In this case, we will choose the option Sum and then run your query.

Sum

You can now see each individual book and also all the individual orders displayed beside the book name.

Individual

Concatenation in Access

We learned the process of normalization, storing information in separate fields. Sometimes you want to see or view data together like combining the first name and last name fields as a single field. You can display that information together by creating a calculated field that concatenates one or more strings. You can also add other characters like a comma or period that you may want.

  • To concatenate in Access, there are two different operators you can use the ampersand (&) and the plus (+) sign.

  • The ampersand will combine two strings into one whereas the plus sign will combine two strings and propagate NULL values, for example, if one value is NULL the entire expression evaluates to null.

Example

Let us take a simple example to understand the process of creating a new query using query design. This is going to be a very simple query that is pulling information from our customers’ table.

Let us now add tblCustomers table close that show table dialog box. We will try some concatenation with some simple named fields.

TblCustomers

Let us now add the first name and last name fields and run the query.

As you can see, the first name and the last name are separated into two different fields.

Table Name

We need to display this information together.

Info Together

Go back to the Design View and in the third field create a new field called full name. Now, add the full name and type the expression that concatenates those two fields together.

Design View Full Name

Let us run the query and you will see a new calculated field.

Calculated Field

It can concatenate the information from these two fields together, but it doesn't exactly appear the way we want it to. It runs all of that text together because it's performing the way we have asked it to. We will now have to ask Access to add space in between the information from these two fields.

Let us now go back to the Design View and add another section to this expression.

Design View Full Name

Add a Space inside quotes and another ampersand. This makes Access take the information from that first name field; add it with a space and then add the information from the last name field at the very end. Now, run the query again and you will see the following results.

Calculated Field

Advertisements
E-Books Store