>

First, Last Functions

Description

Return a field value from the first or last record in the result set returned by a query.

Syntax

First(expr)
Last(expr)

The expr placeholder represents a string expression identifying the field that contains the data you want to use or an expression that performs a calculation using the data in that field. Operands in expr can include the name of a table field, a constant, or a function (which can be either intrinsic or user-defined but not one of the other SQL aggregate or domain aggregate functions).

Remarks

The First and Last functions can be thought of as analogous to the MoveFirst and MoveLast methods of a Recordset object. They simply return the value of a specified field in the first or last record, respectively, of the result set returned by a query. Since records are normally returned in no particular order (unless the query includes an ORDER BY clause), the records returned by these functions will be arbitrary.

See Also

SQL Aggregate Functions.

Specifics (Microsoft Access)

In Microsoft Access, you can use the First and Last functions in the query design grid, in an SQL statement in SQL view of the Query window, or in an SQL statement within Visual Basic code. You can also use the First and Last functions in a calculated control on a form or report.

The First and Last functions are most useful in calculated controls on a report. For example, if you have an Order report that is grouped on a ShipCountry field and sorted on an OrderDate field, you can use the First and Last functions in calculated controls to show the range of earliest to latest order dates for each grouping. To group on the ShipCountry field, click on the Sorting And Grouping button on the Reports toolbar. Choose ShipCountry in the Field/Expression column, and set the GroupHeader and GroupFooter properties to Yes. In Design view, create two new text boxes in the ShipCountry footer, and set the ControlSource property for both to the following expressions.


= First([OrderDate])
= Last([OrderDate])
When you switch to Print Preview, you will see the first and last order dates for each group of orders.

Note

In previous versions of Microsoft Access, the First and Last functions returned the first and last record created, respectively. If you want to return the first or last record in a set of records in Microsoft Access for Windows 95, you should create a query sorted as either Ascending or Descending and set the TopValues property to 1. For more information, see the TopValues property topic. From Visual Basic, you can also create a sorted results set of the query, and use the MoveFirst or MoveLast method to return the first or last record in a set of records.

Example

This example uses the Employees table to return the values from the LastName field of the first and last records returned from the table.


SELECT First(LastName), Last(LastName) FROM Employees;
The next two examples compare using the First and Last functions with simply using the Min and Max functions to find the earliest and latest birth dates of Employees.


SELECT First(BirthDate), Last(BirthDate) FROM Employees;

SELECT Min(BirthDate), Max(BirthDate) FROM Employees;