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
MS Access 2003 - Run-time error 3061 with SQL queries and report
help with trigger/stored procedure
How To Recover Encrypted / Corrupted Word Documents
CInt not working
How do I uninstall Advanced Mass Sebder?
MSDTC Network Issue
How to edit an access(.adp) project using Visual Basic for Applications?
Save unbound combobox column value in Access 2007
Freeze pane on Excel 2007 pivot table
SSRS Remove A User Account