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
Dynamic Controls - checkboxes and tables
Terminal Server and Printing to Mutiple Locations
Open a web page from access 2003
SBS2008 - DCOM was unable to communicate with the computer <computer name> using any of the configured protocols.
5.7.1 Recipient address rejected Access denied, after user name change due to marriage
Managed Folder Policies and Managed Folder Assistant
Reset Autonumbers when moving to production
How to reference the "checked" value of a checkbox in a GridView template field
2 questions about messagequeue
call / run / execute a javascript function on a html webpage with an vb 6 activeX on the same page