April 14, 2010 at 6:42 pm
Hi,
Table
id name date
123 a 2/3/2010
124 b 15/2/2010
125 c 1/1/2010
123 a 12/4/2010
125 c 17/2/2010
this is a table, here id,name,date is composite key.
I want to select id,name and date so that latest date (e.g., for id 123 date 12/4/2010 be selected) is selected for each id and each id and name should be selected once.
So what will be its query?
Thanks
April 14, 2010 at 7:14 pm
duplicate post. Please direct all replies to the other thread at http://www.sqlservercentral.com/Forums/Topic903697-149-1.aspx
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 14, 2010 at 7:34 pm
Please check this sample...
create table #temp (num int, cname varchar(50), dt smalldatetime)
insert #temp select 123, 'a', '2-3-2010 12:35:29'
insert #temp select 123, 'a', '12-4-2010 12:35:29'
select num, cname, max(dt) from #temp group by num, cname
drop table #temp
It grabs the max date which is the latest.
Hope this help expand the logic...
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply