You have seen how aggregate calculations are created in queries and that they summarize data for a group of records. You can also make calculations on a row-by-row basis, using calculated fields. Calculated fields contain customized expressions. The concatenated Meeting field that we made earlier is a simple example of a calculated field. Calculated fields are created and recomputed each time a query is executed.
In the next query, our eventual task is to generate the GPA for each of the students. To determine this, we will need to use both calculated fields and aggregate functions.
GPA values are calculated by the following formula:
Sum(PointValue*CreditHours)/Sum(CreditHours)
Exiting code block.
We'll create our complex calculated field by combining multiple simpler calculations.
Adding the Weighted Field
The first calculation we need is the weighted grade, by multiplying the PointValue of the student's grade by CreditHours available for the course.
Let's begin by opening a query that is partially completed.
Step1. To open the query,
Double-Click qryGPACalc
Step2. To switch to Design View,
Click
Step3. To open a contextual menu, in the query design grid, in the empty column to the right of Credit Hours,
Right-Click the Field row
Step4. To launch the Expression Builder,
Click Build...
NOTE: If Build is grayed out, click another field, then right-click the field row of the empty column again.
Step5. To add the first value to the expression, in the Expression Categories field,
Double-Click PointValue
Step6. To add the multiplication operator, type:
*
Step7. To add the next value to the expression,
Double-Click CreditHours
Step8. To close the Expression Builder,
Click
Step9. To replace the field label, in the query design grid,
Double-Click "Expr1", type: Weighted Enter key
Step10. To view the query result,
Click
Step11. To widen the column, on the right border of the Weighted column,
Double-Click
NOTE: Each student's records may appear in a different order.
Exploring Join Properties
If we examine the enrollments data, we can see the difference can be accounted for by the records where no grade was reported. Why don't these records appear in this dynaset? It's due to the join properties of the relationship between tblGradeScale and tblEnrollments:
The relationship between the tblEnrollments and tblGradeScale tables is established using the Grade field, from tblEnrollments, and the LetterGrade field, from tblGradeScale, as we see in the previous diagram. This relationship is using the join type called inner join. In Access: Structuring & Relating Data, we learned that this type of join only returns the records where the data exists on both sides of the relationship. This means when the Grade field is empty, there isn't a match between the two tables, and these records are excluded from our results.
So what are the records that appear in our results that don't have a PointValue? These are students who received an incomplete in a course. If a student is given an incomplete for a course, the instructor records the grade as an "I." Since the grade "I" appears in both tblEnrollments and tblGradeScale, these records are included in the results. However, there is no point value associated with I grades in tblGradeScale, and if we don't exclude them from the GPA calculation, the results of our calculation will be inaccurate.
Excluding Records Without A PointValue
Let's exclude any records where the PointValue column is blank.
Step1. To return to Design View,
Click
Step2. To specify the appropriate criteria, in the PointValue column,
Click the Criteria field, type: Is Not Null Enter key
Step3. To view the query result,
Click
Step4. To save the changes to this query, press:
Control key + S
Using Aggregate Functions on Calculated Fields
Now that we have eliminated incomplete grades, we need to sum together the rest of the records for each student. Remember, the Totals function allows us to use predefined operations, including Sum, on groups of records.
Remember the equation for GPA is:
Sum(PointValue*CreditHours)/Sum(CreditHours)
Exiting code block.
We created the Weighted calculated column that shows the results of multiplying the PointValue by the Credit Hours. So the equation we are working with at this point is:
Sum(Weighted)/Sum(CreditHours)
What we need to do now is to sum the rows that are for the same student together. So if a student has grades in three classes, the values are added together to give us a sum value for each student's Weighted and CreditHours columns. This will give us the total weighted points the student has earned and the total credit hours each student has taken.
Step1. To return to Design View,
Click
Step2. To turn on the Totals function, in the Show/Hide group of the Query Design tab,
Click
Step3. To select the CreditHours Total row,
Click the CreditHours Total row
Step4. To set the function to sum, in the CreditHours Total row,
Click , Click Sum
Step5. To rename the CreditHours field,
Click before "CreditHours", type: Total Credit Hours: Enter key
Step6. To set the Weighted aggregate calculation,
Click the Weighted Total field, Click , Click Sum
Step7. To view the query result,
Click
Step8. To return to Design View,
Click
NOTE: Setting the Total field to Where isn't required if you do want to group by that field. For example, in the previous query, we added parameter criteria for Rank and also grouped by Rank, so we used Group By.
Step9. To change the Total row setting for PointValue,
Click the Total row, Click , Click Where
Step10. To view the query result,
Click
Step11. To save the changes we have made, press:
Control key + S
Using Results of Aggregate Functions in Calculated Fields
Now that we have the sum total for the values in the Weighted and Total Credit Hours fields for each student, we need to use those values to create another calculated field to divide the value of the Weighted column by the value of the Total Credit Hours column.
Step1. To return to Design View,
Click
Step2. To open a contextual menu, in the query design grid, in the empty column to the right of Weighted,
Right-Click the Field row
Step3. To launch the Expression Builder,
Click Build...
NOTE: If Build is grayed out, click another field, then right-click the field row of the empty column again.
Step4. To start the expression, in Expression Categories list,
Double-Click Weighted
NOTE: If you don't see Weighted, close the Expression Builder and save the query. Then reopen the Expression Builder.
Step5. To add the division symbol, type:
/
Step6. To finish the expression, in Expression Categories list,
Double-Click Total Credit Hours
NOTE: If you don't see Total Credit Hours, close the Expression Builder and save the query. Then reopen the Expression Builder.
Step7. To close the Expression Builder,
Click
Step8. To replace the field label,
Double-Click Expr1, Type: GPA Enter key
Step9. To set the function to Expression, in the Total row under the GPA column,
Click , Click Expression
Step10. To view the query result,
Click
Step11. Expand the GPA column, if necessary.
Formatting a Column
We would like the GPA to have just two decimal places. Let's fix the formatting of the GPA.
Step1. To return to Design View,
Click
Step2. To see the properties for the GPA column, if necessary,
Click an empty part of the column, Click
Step3. To set the format for the field, in the Property Sheet pane,
Click the empty space next to Format, Click , Click Fixed
Step4. To set the number of decimals, in the Decimal Places row, type:
2
Step 5. To close the property sheet pane, in the upper right corner of the pane,
Click
Step6. To view the query result,
Click
Hiding Columns in Datasheet View
We needed the Total Credit Hours and Weighted columns to calculate the GPA, however we don't really need to see them in the query results. It would seem that you could just deselect the Show cbeckbox in the query design for these two columns. However, if we tried this, when we viewed the query, parameter dialogs would appear, prompting us for values for both Total Credit Hours and Weighted. Earlier, we mentioned that Access shows parameter dialogs whenever it doesn't understand an object or expression. Deselecting the Show checkbox actually alters the SQL used in the query. In our case, this would mean Access wouldn't know what the values of Weighted and Total Credit Hours were for the GPA calculation.
There is a way around this: we can hide the columns in Datasheet View. Hiding values in Datasheet View doesn't alter the SQL; instead it sets their width to 0. This means they are hidden from display, but the SQL statement does not change.
Step1. To select both the columns,
Point to the Total Credit Hours column heading, Press & Drag over to Weighted
Step2. To hide the columns, on the Home tab of the Ribbon, in the Records group,
Click , Click Hide Fields
Step3. To save the changes to the query, press:
Control key + S
Understanding Structured Query Language (SQL)
Structured Query Language (SQL) is a standardized data query language that was created for the purpose of working with databases. For those intending to work with server databases such as Oracle and SQL Server, knowledge of SQL is essential. One subset of SQL can be used to form statements that create, retrieve, update, or delete data.
Access automatically translates a query into the Access version of SQL, but it is not necessary to know SQL in order to create queries in Access. Knowing SQL, however, could enable you to customize more powerful Access queries. Also, when working with forms and reports in Access, there will be times when you are asked to save changes to the SQL statement. By exploring the SQL View now, you will be more familiar with the terminology.
We can easily view the SQL version of queries we create. Let's explore the SQL View of this query.
Step1. To select a different view of this query, on the Ribbon,
Click , Click SQL View
Step2. To deselect the text,
Click anywhere in the window
Step3. Close the query.