22
Enhancing Queries Using Functions Introduction To enhance the result produced by a query, you can use some of the built-in functions of Microsoft Access , including those we saw in Lessons 11-14. You can use a function to control the values that would display in the query or you can include the functions in the condition set to filter the values. To control how the values would display in the query, start a query in Design View or open a query in Design View. In the bottom section of the window, in the box of the field name, type the expression. For example, if you have a column named Gender and that display the genders as Male or as Female but you want to display only M or F respectively, you can use the Left() function in an expression as Left(Gender, 1):

Enhancing Queries

Embed Size (px)

DESCRIPTION

Access

Citation preview

Page 1: Enhancing Queries

Enhancing QueriesUsing FunctionsIntroduction

To enhance the result produced by a query, you can use some of the built-in functions of Microsoft Access, including those we saw in Lessons 11-14. You can use a function to control the values that would display in the query or you can include the functions in the condition set to filter the values.

To control how the values would display in the query, start a query in Design View or open a query in Design View. In the bottom section of the window, in the box of the field name, type the expression. For example, if you have a column named Gender and that display the genders as Male or as Female but you want to display only M or F respectively, you can use the Left() function in an expression as Left(Gender, 1):

Page 2: Enhancing Queries

In the same way, you can use any of the functions we have seen so far.

To use a function in a criterion, open the query in Design View and select the column(s) you want. In the lower section of the window, click the Criteria box that corresponds to the column that will hold the criterion and type the expression that includes the function. For example, on a list of students that includes their dates of birth in a column named DOB, to get the list of students born in 1994, you would set the condition as Year([DOB])=1994. Here is an example:

Page 3: Enhancing Queries

Summary QueriesIntroduction

Consider the following list of students:

Page 4: Enhancing Queries

There are various types of statistics you may want to get from this list: you may want to know the number of students registered in the school, you may want to know the number of girls or the number boys, you may want to know how many students were born in each year, you may want to know the average age of the students, you may want to know the oldest or the youngest students, or you may want to know the number of students from each ZIP code. To assist you with getting these statistics, Microsoft Access provides a type of query called a summary query.

A query is referred to as summary if it provides one or various analytic statistics about the records.

Practical Learning: Introducing Summary Queries

1. Start Microsoft Access

2. From the resources that accompany our lessons, open the Bethesda Car Rental1 database

3. On the Ribbon, click Create and, in the Queries section, click Query Design

4. In the Show Tables dialog box, click Company Assets, click Add, and click Close

5. In the top section of the window, double-click Category

6. Double-click Category again

7. Double-click Purchase Price

8. Double-click Purchase Price again

Creating a Summary Query

As always, when creating a query, you can use the Query Wizard or display one in Design View. If

Page 5: Enhancing Queries

you are working in Design View, to make a query summarize its data:

In the Show/Hide section of the Ribbon, click the Totals button

Right-click the bottom section of the query window and click Totals

After applying the Totals feature, each column of the query would become equipped with a row named Total.

Although you can create a summary query with all the fields or any field(s) of a query, the purpose of the query is to summarize data, not to review the records, which you would do with a normal select query. For a good summary query, you should select a column where the records hold categories of data. This means that the records in the resulting list have to be grouped by categories.

To support summary queries, the SQL provides the GROUP BY clause. It means where the records display, they would be grouped by their categories. For example, if you want to get the number of students by gender from a list of students, you would select the column that holds that information, but you can select other columns also:

When the results come up, they would be grouped by their categories:

Page 6: Enhancing Queries

As stated already, the purpose of a summary query is to provide some statistics. Therefore, it is normal that you be interested only in the column(s) that hold(s) the desired statistics and avoid the columns that are irrelevant:

Page 7: Enhancing Queries

If you select (only) the one column that holds the information you want, in the resulting list, each of its categories would display only once:

Summarizing the Values

To get the types of statistics you want, in the Design View of the query, add the same column one more time, and click the Total box that corresponds to the column:

In reality, a summary query uses some of the functions that ship with Microsoft Access:

Page 8: Enhancing Queries

Count: Microsoft Access uses the Count() function to count the number of occurrences of the category in the column and produces the total

SELECT Students.Gender, Count(Students.Gender) AS CountOfGenderFROM StudentsGROUP BY Students.Gender;

First: Microsoft Access uses the First() function to get the first occurrence of the value in the category

Page 9: Enhancing Queries

Last: Microsoft Access uses the Last() function to get the last occurrence of the value in the category

Consider the following table that shows the list of employees and the time they worked:

Page 10: Enhancing Queries

Consider the following query that wants to summarize the time worked:

Page 11: Enhancing Queries
Page 12: Enhancing Queries

If the column holds numeric values:

o Sum: The Sum() function is used to sum up the values in the category

SELECT TimeKeepers.EmployeeNumber,Sum(TimeKeepers.TimeWorked) AS SumOfTimeWorkedFROM TimeKeepers

Page 13: Enhancing Queries

GROUP BY TimeKeepers.EmployeeNumber;

o Avg: The sum of values in a category would be divided by the number of occurrences in that category to get the average. Here is an example:

o SELECT TimeKeepers.EmployeeNumber,o Avg(TimeKeepers.TimeWorked) AS AvgOfTimeWorkedo FROM TimeKeepers

GROUP BY TimeKeepers.EmployeeNumber;

o Min: The lowest value of the category would be produced from the Min() function

Page 14: Enhancing Queries

The result is the lowest time worked for each employee:

o Max: The highest value of the category would be produced using the Max() function. Here is an example:

Page 15: Enhancing Queries

SELECT TimeKeepers.EmployeeNumber,Min(TimeKeepers.TimeWorked) AS MinOfTimeWorked,Max(TimeKeepers.TimeWorked) AS MaxOfTimeWorkedFROM TimeKeepersGROUP BY TimeKeepers.EmployeeNumber;

o StdDev: The StDev() function is used to calculate the standard deviation of all numeric values of a group. If there is no value or the same value in the considered group, this function returns NULL. This means that there should be at least two different values in the group. Here is an example:

Page 16: Enhancing Queries

SELECT TimeKeepers.EmployeeNumber,StDev(TimeKeepers.TimeWorked) AS StDevOfTimeWorkedFROM TimeKeepersGROUP BY TimeKeepers.EmployeeNumber;

The standard deviation of the values of the category would be calculated using the StdDev() function

o Var: The Var() function calculates the statistical variance of all numeric values of a group. If there is no value or the same value in the considered group, this function returns NULL. Here is an example:

o SELECT TimeKeepers.EmployeeNumber,o Var(TimeKeepers.TimeWorked) AS VarOfTimeWorkedo FROM TimeKeeperso GROUP BY TimeKeepers.EmployeeNumber;

Page 17: Enhancing Queries

The statistical variance of the values in the category would be calculated

Expression: Consider the following query that we saw earlier:

Imagine that you want to get the percentage of occurrences of each gender. To get such a value, you can write an expression such as (Count(Gender) / 164) * 100. If you want to do this in a summary query, you can specify the Total type as Expression. Here is an example:

SELECT Students.Gender,Count(Students.Gender) AS CountOfGender,(Count([Gender])/164)*100 AS Percentage

Page 18: Enhancing Queries

FROM StudentsGROUP BY Students.Gender;

Practical Learning: Summarizing

1. In the bottom section of the window, change the header of the second column to Qty: Category

2. Click its Total combo box and select Count

3. Change the header of the third column to Total Spent: Purchase Price

4. Click its Total combo box and select Sum

5. Change the header of the fourth column to Average: Purchase Price

6. Click its Total combo box and select Avg

7. Right-click the query and click SQL View

8. SELECT [Company Assets].[Asset Type], 9. Count([Company Assets].[Asset Type]) AS Qty,

Page 19: Enhancing Queries

10. Sum([Company Assets].[Purchase Price]) AS [Total Spent], 11. Avg([Company Assets].[Purchase Price]) AS Average12. FROM [Company Assets]

GROUP BY [Company Assets].[Asset Type];

13. Right-click the title bar and click Datasheet View

14. Close the query

15. When asked whether you want to save, click No

16. Open the Altair Realtors2 database

17. On the Ribbon, click Create

18. In the Queries section, click Query Design

19. In the Show Tables dialog box, double-click Properties and click Close

20. On the Ribbon, click the Totals button

21. In the list of fields, double-click Property Type, Market Value, Market Value, and Property Type

22. In the bottom section of the window, change the header of the second column to Cheapest: Market Value

23. Click its Total combo box and select Min

24. Change the header of the third column to Most Expensive: Market Value

25. Click its Total combo box and select Max

26. For the fourth column, set its Total to Where (clear its Show check box if necessary)

Page 20: Enhancing Queries

27. Set its Criteria to Is Not Null and press Enter

28. Right-click the query and click SQL View

29. SELECT Properties1.[Property Type], 30. Min(Properties1.[Market Value]) AS Cheapest, 31. Max(Properties1.[Market Value]) AS [Most Expensive]32. FROM Properties133. WHERE (((Properties1.[Property Type]) Is Not Null))

GROUP BY Properties1.[Property Type];

34. Right-click the title bar and click Datasheet View

35. Close the query

36. When asked whether you want to save, click No