Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Max(Date) ??

Author  Topic 

luridjester
Starting Member

2 Posts

Posted - 2011-03-16 : 11:05:14
I've got a query that I run periodically but I'm finding some issues with it that I've been unable to resolve on my own.

I've simplified the output to show what I'm trying to get at, but I'm trying to pull distinct values for the employee name, their employee number and the most recent DateIn value so the second entry for Bob should (ideally) be left off since a more recent entry is available.

EmpName EmpNum DateIn
Bob 1234 3/15/2011
Bob 1234 3/14/2011
Jim 2345 3/15/2011
Sue 3456 3/15/2011

So here's an extremely basic version of my query:

Select distinct
EmpName
,Empnum
,DateIn

From EmpDB
Where DateIn >= 1/1/2011

Group By
EmpName
,Empnum
,DateIn


Thanks in advance

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-16 : 11:35:21
Select
EmpName
,Empnum
,MAX(DateIn) as DateIn

From EmpDB
Where DateIn >= 1/1/2011

Group By
EmpName
,Empnum


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

luridjester
Starting Member

2 Posts

Posted - 2011-03-16 : 12:04:13
quote:
Originally posted by jimf

Select
EmpName
,Empnum
,MAX(DateIn) as DateIn

From EmpDB
Where DateIn >= 1/1/2011

Group By
EmpName
,Empnum


Jim

Everyday I learn something that somebody else already knew



I swear I tried that and it wasn't working, but now when I do it again it's working fine. Thanks for that, I expected it to be much more complicated than it was after looking at some of the threads with similar questions.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-16 : 12:15:01
You're welcome. I bet you included the DateIn in your group by column in your original query.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -