Vous êtes sur la page 1sur 13

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):

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:

mmary Queries

oduction

sider the following list of students:

mmarizing the Values

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

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

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

SELECT Students.Gender, Count(Students.Gender) AS CountOfGender


FROM Students
GROUP BY Students.Gender;

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

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:

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

If the column holds numeric values:


o

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

ractical 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,
10.
Sum([Company Assets].[Purchase Price]) AS [Total Spent],
11.
Avg([Company Assets].[Purchase Price]) AS Average
12. 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)

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
Properties1
33. 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

Vous aimerez peut-être aussi