dbTalk Databases Forums  

Select Most Recent

comp.databases.ms-access comp.databases.ms-access


Discuss Select Most Recent in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gilgamesh
 
Posts: n/a

Default Select Most Recent - 02-05-2011 , 02:57 PM






Hi,
I have a table of people's degrees. Its fields are:

ID (the id of the person)
OrgID (the ID of the college that awarded the degree)
Degree (usually BA, BS, etc )
StudyField ( something like "Political Science" "Physics," etc)
DegDt (the date the degree was awarded

I have a query I'm using to find the most recent degree of the person
(some people have three or four):

SELECT DISTINCT tblExternalDegrees.ID, tblExternalDegrees.[Org ID],
tblExternalDegrees.Degree, tblExternalDegrees.[StudyField],
tblExternalDegrees.[DegDt]
FROM tblExternalDegrees
WHERE (((tblExternalDegrees.[Deg Dt])=(select Max([Deg Dt]) from
tblExternalDegrees as Ext where Ext.ID = tblExternalDegrees.ID)));

I use the date the degree was awarded to find the most recent. It
works just fine for nearly all the people in the database.
Unfortunately, some people have two degrees awarded on the same date
from the same institution. This occurs when a double major was
recorded as two degrees or sometimes when a person had done enough to
get a BA and an MA from the same institution but they were awarded at
the same time. In those cases, my query above finds both.
What I need is to just find one of the two. It does not really matter
which of the two, but I need to return just one degree per person. I
need to modify the query to return the most recent degree and if two
were awarded on the same day, return just one of them. I cannot get
my head round a way to do that.

Any help appreciated.

Mike

Reply With Quote
  #2  
Old   
Marshall Barton
 
Posts: n/a

Default Re: Select Most Recent - 02-05-2011 , 10:36 PM






Gilgamesh wrote:
Quote:
I have a table of people's degrees. Its fields are:

ID (the id of the person)
OrgID (the ID of the college that awarded the degree)
Degree (usually BA, BS, etc )
StudyField ( something like "Political Science" "Physics," etc)
DegDt (the date the degree was awarded

I have a query I'm using to find the most recent degree of the person
(some people have three or four):

SELECT DISTINCT tblExternalDegrees.ID, tblExternalDegrees.[Org ID],
tblExternalDegrees.Degree, tblExternalDegrees.[StudyField],
tblExternalDegrees.[DegDt]
FROM tblExternalDegrees
WHERE (((tblExternalDegrees.[Deg Dt])=(select Max([Deg Dt]) from
tblExternalDegrees as Ext where Ext.ID = tblExternalDegrees.ID)));

I use the date the degree was awarded to find the most recent. It
works just fine for nearly all the people in the database.
Unfortunately, some people have two degrees awarded on the same date
from the same institution. This occurs when a double major was
recorded as two degrees or sometimes when a person had done enough to
get a BA and an MA from the same institution but they were awarded at
the same time. In those cases, my query above finds both.
What I need is to just find one of the two. It does not really matter
which of the two, but I need to return just one degree per person. I
need to modify the query to return the most recent degree and if two
were awarded on the same day, return just one of them. I cannot get
my head round a way to do that.

Assuming you have a primary key field in this table, I think
this might do it:

SELECT ID, [Org ID], Degree, StudyField, DegDt
FROM tblExternalDegrees As T
WHERE PK = (SELECT TOP 1 PK
FROM tblExternalDegrees as Ext
WHERE Ext.ID = T.ID
ORDER BY DegDt DESC, Degree DESC,
StudyField DESC)

--
Marsh

Reply With Quote
  #3  
Old   
Gilgamesh
 
Posts: n/a

Default Re: Select Most Recent - 02-07-2011 , 03:24 PM



On Feb 5, 8:36*pm, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote:
Quote:
Gilgamesh wrote:
I have a table of people's degrees. *Its fields are:

ID *(the id of the person)
OrgID (the ID of the college that awarded the degree)
Degree *(usually BA, BS, etc *)
StudyField * ( something like "Political Science" "Physics," etc)
DegDt *(the date the degree was awarded

I have a query I'm using to find the most recent degree of the person
(some people have three or four):

SELECT DISTINCT tblExternalDegrees.ID, tblExternalDegrees.[Org ID],
tblExternalDegrees.Degree, *tblExternalDegrees.[StudyField],
tblExternalDegrees.[DegDt]
FROM tblExternalDegrees
WHERE (((tblExternalDegrees.[Deg Dt])=(select Max([Deg Dt]) from
tblExternalDegrees as Ext where Ext.ID = tblExternalDegrees.ID)));

I use the date the degree was awarded to find the most recent. *It
works just fine for nearly all the people in the database.
Unfortunately, some people have two degrees awarded on the same date
from the same institution. *This occurs when a double major was
recorded as two degrees or sometimes when a person had done enough to
get a BA and an MA from the same institution but they were awarded at
the same time. *In those cases, my query above finds both.
What I need is to just find one of the two. *It does not really matter
which of the two, but I need to return just one degree per person. *I
need to modify the query to return the most recent degree and if two
were awarded on the same day, return just one of them. *I cannot get
my head round a way to do that.

Assuming you have a primary key field in this table, I think
this might do it:

SELECT ID, [Org ID], Degree, StudyField, DegDt
FROM tblExternalDegrees As T
WHERE PK = (SELECT TOP 1 PK
* * * * * * * * * * * * *FROM tblExternalDegrees as Ext
* * * * * * * * * * * * *WHERE Ext.ID = T.ID
* * * * * * * * * * * * *ORDER BY DegDt DESC, Degree DESC,
StudyField DESC)

--
Marsh
Marsh,
Thanks. That was just what I needed.
Michael

Reply With Quote
Reply




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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.