Most recent Record on a date field (1 Viewer)

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
I am having trouble trying to get the most recent record. heres my query
i get no results on this query. Basically what i want is if CMTCH has 4 records over 1 year. I want the most recent record.
Example
10-1-12
8-1-12
2-15-12
5-25-12

I would want the records with the 10-1-12 date.

select

a.sumjobcode, a.sumEffDate, a.sumFinalPoints,
b.jobtitle, b.DSCRIPTN
FROM tblSummary a
left outer join vDepartments b
on a.sumJobCode = b.JOBTITLE
where a.sumEffDate =(
SELECT MAX(a.sumEffDate)
FROM tblsummary a)
and
a.sumJobCode = 'CMTCH'
 

mdlueck

Sr. Application Developer
Local time
Today, 05:26
Joined
Jun 23, 2011
Messages
2,631
I do not quite follow the logic of your query.

To simply SELECT "Most recent Record on a date field" I would do something along the lines of...

Code:
SELECT TOP 1 ....
WHERE ...
ORDER BY [datecol] DESC
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
what if my date i want isnt always the top row.
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
Nevermind i get where you are coming from, i think that will work. i overlooked your order by.
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
I tried using the max but i would get more then 1 result
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
That is what i have, i get 4 records returned. All with 4 different dates, i want the most recent record.
select
a.sumjobcode, max(a.sumEffDate), a.sumFinalPoints,
b.jobtitle, b.DSCRIPTN
FROM tblSummary a
left outer join vDepartments b
on a.sumJobCode = b.JOBTITLE
where a.sumJobCode = 'CMTCH'
group by a.sumJobCode, a.sumEffDate, a.sumFinalPoints, b.JOBTITLE, b.DSCRIPTN
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
can you display that as an example? im confused
 

mdlueck

Sr. Application Developer
Local time
Today, 05:26
Joined
Jun 23, 2011
Messages
2,631
Did my air code in #2 fail to deliver the desired results in some way?
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
No that works, im just interested in the max function too.
 

rkrause

Registered User.
Local time
Today, 02:26
Joined
Sep 7, 2007
Messages
343
Im posting this in a sql forum, so im using SSMS not accesss...
 

SQL_Hell

SQL Server DBA
Local time
Today, 10:26
Joined
Dec 4, 2003
Messages
1,360
Please use the example in post 2 by mdlueck, do not subquery in the same table using MAX, it's will be badly performing code compared to TOP 1.
 

Users who are viewing this thread

Top Bottom