Hi John,
I disagree on the use of First and Last. As example, try to do the same,
in MS SQL Server:
Table1 ' table name
f1, f2, f3 ' fields name
1 99 1
1 1 99
1 50 50
SQL:
SELECT f1, FIRST(f2), FIRST(f3) FROM Table1 GROUP BY f1 ;
In Jet, the result would be ANY of the three record. By opposition,
for illustration, if you use MIN or MAX, you would get something like:
1 99 99
but f2+f3 <> 100 illustrates the fact they do not come, both (f2
and f3), from the same existing record. So, what would it be in MS SQL
Server (who does not have FIRST, neither LAST) ? (and I am a very bad guy
in this case, I don't supply a primary key... )
So, there are cases where FIRST and LAST are important. A more concrete
example is to "remove" duplicated values. If field f1 define the "group"
(say, the clientID) while f2 is the phone number and f3 the address, we
want to keep and we want ONE record per clientID, and but we don't want to
mix the phone number in Texas with an address in Washington. The Jet
statement is exactly like the simple one I typed... but without
FIRST/LAST... ?
I agree on the "poor" choice of word, they have the tendency to be read
as "earliest" and "latest", while they are not.
Isn't there an equivalent in Oracle (for "get from the same row") ?
Vanderghast, Access MVP
John Viescas said:
David-
If your ORDER BY solution works, then Min and Max should also work -- but
the OP says this isn't so. First and Last are just a kludge to get a random
value from the group when you don't want to use one of the other aggregate
functions. They should never be used, IMO.
--
John Viescas, author
"Microsoft Office Access 2003 Inside Out" (coming soon)
"Running Microsoft Access 2000"
"SQL Queries for Mere Mortals"
http://www.viescas.com/
(Microsoft Access MVP since 1993)
GreySky said:
I bet you're missing the magical TOP 100 PERCENT subquery
that is required in order for First and Last to work
predictably.
The first thing you must do is set a query that determines
the sort order.
For example:
SELECT TOP 100 PERCENT * From tblMeterReading ORDER BY
[Equip #], [Last Meter]
--- Now use this query as the base query for your first()
and last() query.
The bottom line is *if you don't specify Top* the Order By
is ignored when it's used as a subquery.
David Atkins, MCP