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
Find & Replace on all views, stored procedures, functions in a given database/server
C# ,framework 2.0 , bindingsource filter question
Infopath Data to SQL
recovering *Contacts* from an old .OST file???
Exclude all records from a join which have two specific values in one table
Visual Studio 2008 Pro Trial does work with SQL 2008 CTP Trial
Feed query results from one form to another
Default Message Required Field
Jet-SQL REPLACE bug?
word 2003, the password is forgotten, how open the document