Microsoft
Software
Hardware
Network
Question : SQL query to select distinct ID for latest date past June/01/2009 where there are (possibly) more than one occurrence of each ID
I have a table with (to keep this simple) two columns : myID and myDate
MyID is NOT unique: that is, the same ID can occur more than once in the MyID field.
MyDate may be blank in some rows
I'm trying to write a query which gives me only the most recent occurence of any ID with an entry in the MyDate field, where the date is greater than June 01, 2009.
So far I've come up with this code.
SELECT DISTINCT(myTable.[MyID]), myTable.[myDate]
FROM
(
SELECT myTable.[myID],MAX(myTable
.[myDate])
AS `latest`
FROM myTable
HAVING myTable.[myDate]>#6/1/2009
#
)
;
Here is my sample table.
myID myDate
123 1/20/2004
123 3/18/2009
123 8/16/2009
123 8/17/2009
221 5/22/2006
229
253
284 10/19/2009
The query should only give me two rows as a result:
123 8/17/2009
284 10/19/2009
Help?
Answer : SQL query to select distinct ID for latest date past June/01/2009 where there are (possibly) more than one occurrence of each ID
also you may add null condition:
select myid, max(mydate) maxdate
from mytable
where mydate > #6/1/2009# and mydate is not null
group by myid
order by myid
Random Solutions
How to zip/unzip with VB.NET?
Antivirus solution for Windows Server 2008 R2 Core.
Multiple Combo Box Selection in Microsoft Access
Terminal Server port configuration
Caluculate if met SLA for completetion of task
Passing parameter by open args
Concatenate ntext field with text field in SQL view
.net User Control Events
WIndows 2003 Server Cluster without any External Storage
Text Cursor Starts At Top of Page in Microsoft Office 2003