A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

First|Last Function



 
 
Thread Tools Display Modes
  #1  
Old February 13th, 2007, 07:47 PM posted to microsoft.public.access.queries
May
external usenet poster
 
Posts: 6
Default First|Last Function

Field “code” is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the “Code” for each ID. I tried to use Function “First”
or “Last” in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305


  #2  
Old February 13th, 2007, 08:18 PM posted to microsoft.public.access.queries
Jeff Boyce
external usenet poster
 
Posts: 8,621
Default First|Last Function

Your definitions of "First" and "Last" are probably not the same as Access'
definitions.

If you mean you want the "smallest" and "largest", try using Minimum and
Maximum.

Regards

Jeff Boyce
Microsoft Office/Access MVP

"May" wrote in message
...
Field "code" is a text variable in a table and it can be sorted properly
in
the same way as a numeric variable. In a query, I want the dataset shows
the
first record ONLY in the "Code" for each ID. I tried to use Function
"First"
or "Last" in query design grid after I first sorted the records, it did
not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305
.



  #3  
Old February 13th, 2007, 09:20 PM posted to microsoft.public.access.queries
May
external usenet poster
 
Posts: 6
Default First|Last Function

Hi, Jeff:

Thank you for your help. I did not mean the numeric number for the largest
or smallest; I mean the FIRST or LAST record in a dataset.

I need to get only one record and the first one for each ID in a couple of
records (the # of records depends on the ID’s activity). As required, the
query produces a dataset which has distinct records.

Does the “First” and “Last” function in Access mean the First and Last
record in a dataset? If not, what I should do?

Thanks for help.

"May" wrote:

Field “code” is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the “Code” for each ID. I tried to use Function “First”
or “Last” in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305


  #4  
Old February 13th, 2007, 11:42 PM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default First|Last Function

First and Last do mean the first and last in the query's
**source** dataset. Another thing you need to be aware of
is that a table's records are ***not*** ordered. This means
that without one or more fields in your table that can be
used to create a unique sort, there is no such thing as the
first or last record for any subset of records.

If you do have a set of fields in the table for a unique
sort order, then you can use a query that sorts the records
as the basis for your query.
--
Marsh
MVP [MS Access]


May wrote:
Thank you for your help. I did not mean the numeric number for the largest
or smallest; I mean the FIRST or LAST record in a dataset.

I need to get only one record and the first one for each ID in a couple of
records (the # of records depends on the IDs activity). As required, the
query produces a dataset which has distinct records.

Does the First and Last function in Access mean the First and Last
record in a dataset? If not, what I should do?

"May" wrote:

Field code is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the Code for each ID. I tried to use Function First
or Last in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305



  #5  
Old February 14th, 2007, 05:04 PM posted to microsoft.public.access.queries
May
external usenet poster
 
Posts: 6
Default First|Last Function

Marshall,

Thank you for telling me that the records in a table are not ordered. Should
I try subquery?

"Marshall Barton" wrote:

First and Last do mean the first and last in the query's
**source** dataset. Another thing you need to be aware of
is that a table's records are ***not*** ordered. This means
that without one or more fields in your table that can be
used to create a unique sort, there is no such thing as the
first or last record for any subset of records.

If you do have a set of fields in the table for a unique
sort order, then you can use a query that sorts the records
as the basis for your query.
--
Marsh
MVP [MS Access]


May wrote:
Thank you for your help. I did not mean the numeric number for the largest
or smallest; I mean the FIRST or LAST record in a dataset.

I need to get only one record and the first one for each ID in a couple of
records (the # of records depends on the ID’s activity). As required, the
query produces a dataset which has distinct records.

Does the “First” and “Last” function in Access mean the First and Last
record in a dataset? If not, what I should do?

"May" wrote:

Field “code” is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the “Code” for each ID. I tried to use Function “First”
or “Last” in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305




  #6  
Old February 14th, 2007, 06:39 PM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default First|Last Function

A subquery (instead of a separate query) might work, but the
critical point is being able to use a unique sort order to
determine "first" and "last".

I'd be happy to suggest something but you have to explain
how to use what fields to create the unique sorting.
--
Marsh
MVP [MS Access]


May wrote:
Thank you for telling me that the records in a table are not ordered. Should
I try subquery?

"Marshall Barton" wrote:
First and Last do mean the first and last in the query's
**source** dataset. Another thing you need to be aware of
is that a table's records are ***not*** ordered. This means
that without one or more fields in your table that can be
used to create a unique sort, there is no such thing as the
first or last record for any subset of records.

If you do have a set of fields in the table for a unique
sort order, then you can use a query that sorts the records
as the basis for your query.


May wrote:
Thank you for your help. I did not mean the numeric number for the largest
or smallest; I mean the FIRST or LAST record in a dataset.

I need to get only one record and the first one for each ID in a couple of
records (the # of records depends on the IDs activity). As required, the
query produces a dataset which has distinct records.

Does the First and Last function in Access mean the First and Last
record in a dataset? If not, what I should do?

"May" wrote:

Field code is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the Code for each ID. I tried to use Function First
or Last in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305


  #7  
Old February 14th, 2007, 10:01 PM posted to microsoft.public.access.queries
May
external usenet poster
 
Posts: 6
Default First|Last Function

Here is the code I used before. I did not get what I wanted. Your help is
much appreciated. Field "Term" is text not number.

A scenario could be like this: a student could be admitted more than once in
the same program (withdrew then back) and transferred among different campus.
To determine if a student is promoted to a higher level is to check the
enrollment to a required class (Class). The earliest Term a student took
either HIST316 or DRAM300 is a level 3 student. The query should keep the
first Enrollment Date; the first enrolled CampusName, the first Term this
person is promoted to level 3 (the term a student took the required course),
and the course number.

ID CampusName EnrollDate Subject Level Term Class Mark
1 A Sep-96 HIST 3 199909 316 48
1 B Sep-96 HIST 3 200009 316 61
2 B Sep-97 HIST 3 199809 316 56
2 B Sep-99 HIST 3 200109 316 68
3 C Sep-96 HIST 3 199809 316 W
3 C Sep-99 HIST 3 199909 316 58
4 A Sep-96 HIST 3 199809 316 W
4 C Sep-96 HIST 3 200009 316 58
4 C Sep-96 DRAM 3 200209 300 58
5 D Sep-96 DRAM 3 200409 300 63
5 D Sep-00 DRAM 3 200409 300 63


What I wanted is as follows:
ID CampusName EnrollDate Subject Level Term Class Mark
1 A Sep-96 HIST 3 199909 316 48
2 B Sep-97 HIST 3 199809 316 56
3 C Sep-96 HIST 3 199809 316 W
4 A Sep-96 HIST 3 199809 316 W
5 D Sep-96 DRAM 3 200409 300 63


SELECT DISTINCT tblStudent.StudntID, tblAdmission.CampusName,
Min(tblAdmissionion.EnrollDate) AS MinOfEnrollDate, tblStudent.Level,
tblStudentCourse.Term, tblStudentCourse.CoursNum, tblStudentCourse.Grade

FROM ((tblStudent INNER JOIN tblStudentCourse ON tblStudent.StudntID =
tblStudentCourse.StudntID) INNER JOIN tblAdmission ON tblStudent.StudntID =
tblAdmission.StudntID) INNER JOIN tblStudentName ON tblStudent.StudntID =
tblStudentName.StudntID

WHERE (((tblStudentCourse.Subject)="HIST" Or
(tblStudentCourse.Subject)="DRAM") AND ((tblStudentCourse.CoursNum)="316"))
Or (tblStudentCourse.CoursNum)="300"))
ORDER BY Min(tblAdmission.AdmDate)


"Marshall Barton" wrote:

A subquery (instead of a separate query) might work, but the
critical point is being able to use a unique sort order to
determine "first" and "last".

I'd be happy to suggest something but you have to explain
how to use what fields to create the unique sorting.
--
Marsh
MVP [MS Access]


May wrote:
Thank you for telling me that the records in a table are not ordered. Should
I try subquery?

"Marshall Barton" wrote:
First and Last do mean the first and last in the query's
**source** dataset. Another thing you need to be aware of
is that a table's records are ***not*** ordered. This means
that without one or more fields in your table that can be
used to create a unique sort, there is no such thing as the
first or last record for any subset of records.

If you do have a set of fields in the table for a unique
sort order, then you can use a query that sorts the records
as the basis for your query.


May wrote:
Thank you for your help. I did not mean the numeric number for the largest
or smallest; I mean the FIRST or LAST record in a dataset.

I need to get only one record and the first one for each ID in a couple of
records (the # of records depends on the ID’s activity). As required, the
query produces a dataset which has distinct records.

Does the “First” and “Last” function in Access mean the First and Last
record in a dataset? If not, what I should do?

"May" wrote:

Field “code” is a text variable in a table and it can be sorted properly in
the same way as a numeric variable. In a query, I want the dataset shows the
first record ONLY in the “Code” for each ID. I tried to use Function “First”
or “Last” in query design grid after I first sorted the records, it did not
work.

Would you please help me with an example for this?

ID Code
1 122005
1 122305
1 122309
2 122406
2 122409
2 122409
3 122005
3 122305



  #8  
Old February 15th, 2007, 12:34 AM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default First|Last Function

May wrote:

Here is the code I used before. I did not get what I wanted. Your help is
much appreciated. Field "Term" is text not number.

A scenario could be like this: a student could be admitted more than once in
the same program (withdrew then back) and transferred among different campus.
To determine if a student is promoted to a higher level is to check the
enrollment to a required class (Class). The earliest Term a student took
either HIST316 or DRAM300 is a level 3 student. The query should keep the
first Enrollment Date; the first enrolled CampusName, the first Term this
person is promoted to level 3 (the term a student took the required course),
and the course number.

ID CampusName EnrollDate Subject Level Term Class Mark
1 A Sep-96 HIST 3 199909 316 48
1 B Sep-96 HIST 3 200009 316 61
2 B Sep-97 HIST 3 199809 316 56
2 B Sep-99 HIST 3 200109 316 68
3 C Sep-96 HIST 3 199809 316 W
3 C Sep-99 HIST 3 199909 316 58
4 A Sep-96 HIST 3 199809 316 W
4 C Sep-96 HIST 3 200009 316 58
4 C Sep-96 DRAM 3 200209 300 58
5 D Sep-96 DRAM 3 200409 300 63
5 D Sep-00 DRAM 3 200409 300 63


What I wanted is as follows:
ID CampusName EnrollDate Subject Level Term Class Mark
1 A Sep-96 HIST 3 199909 316 48
2 B Sep-97 HIST 3 199809 316 56
3 C Sep-96 HIST 3 199809 316 W
4 A Sep-96 HIST 3 199809 316 W
5 D Sep-96 DRAM 3 200409 300 63


SELECT DISTINCT tblStudent.StudntID, tblAdmission.CampusName,
Min(tblAdmissionion.EnrollDate) AS MinOfEnrollDate, tblStudent.Level,
tblStudentCourse.Term, tblStudentCourse.CoursNum, tblStudentCourse.Grade

FROM ((tblStudent INNER JOIN tblStudentCourse ON tblStudent.StudntID =
tblStudentCourse.StudntID) INNER JOIN tblAdmission ON tblStudent.StudntID =
tblAdmission.StudntID) INNER JOIN tblStudentName ON tblStudent.StudntID =
tblStudentName.StudntID

WHERE (((tblStudentCourse.Subject)="HIST" Or
(tblStudentCourse.Subject)="DRAM") AND ((tblStudentCourse.CoursNum)="316"))
Or (tblStudentCourse.CoursNum)="300"))
ORDER BY Min(tblAdmission.AdmDate)



Well., this sure clarifies the issue. Just goes to show how
we can waste time chasing down a mistaken guess of how to do
something instead of just solving the original problem.

We can forget about the First/Last questions use this kind
of (untested) query:

SELECT tblStudent.StudntID,
tblAdmission.CampusName,
tblAdmissionion.EnrollDate AS EarliestEnrollDate,
tblStudent.Level,
tblStudentCourse.Term,
tblStudentCourse.CoursNum,
tblStudentCourse.Grade
FROM ((tblStudent INNER JOIN tblStudentCourse
ON tblStudent.StudntID = tblStudentCourse.StudntID)
INNER JOIN tblAdmission
ON tblStudent.StudntID = tblAdmission.StudntID)
INNER JOIN tblStudentName
ON tblStudent.StudntID = tblStudentName.StudntID
WHERE ((tblStudentCourse.Subject)="HIST"
And tblStudentCourse.CoursNum)="316")
Or (tblStudentCourse.Subject)="DRAM"
And tblStudentCourse.CoursNum)="300"))
And tblAdmissionion.EnrollDate =
(SELECT Min(X.EnrollDate)
FROM tblStudent As S
INNER JOIN tblStudentCourse As C
ON S.StudntID = C.StudntID
WHERE S.StudntID = tblStudent.StudntID
And S.Level = tblStudent.Level
And C.Subject = tblStudentCourse.Subject
And C.CoursNum = tblStudentCourse.CoursNum)
ORDER BY Min(tblAdmission.AdmDate)

I'm not sure I got the subquery's Joins right, but I didn't
see a reason for including tables not needed(?) to find the
enroll dates.

--
Marsh
MVP [MS Access]
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:31 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright 2004-2024 OfficeFrustration.
The comments are property of their posters.