Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 last modified record from duplicate

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-11-03 : 00:58:40
Hi all,

I have 2 column ID and Date

ID |date
1 |2011-10-26 23:59:18.000
1 |2011-10-27 00:06:31.000
1 |2011-10-27 03:02:57.000
2 |2011-09-27 03:02:57.000
3 |2011-11-01 00:49:12.000
3 |2011-11-02 23:16:53.000

I need to select distinct record of ID which is latest based on Date column.

Result:
ID |date
1 |2011-10-27 03:02:57.000
2 |2011-09-27 03:02:57.000
3 |2011-11-02 23:16:53.000

Thanks,
Gangadhara MS
SQL Developer and DBA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-03 : 01:03:51
SELECT ID, MAX([date])
FROM YourTable
GROUP BY ID
ORDER BY ID

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-11-03 : 01:44:36
Hi Tara,

Thanks for the response,actually one more fields also there sameple is here

ID |date | ssemail
1 |2011-10-26 23:59:18.000 |a@tt.com
1 |2011-10-27 00:06:31.000 |b@tt.com
1 |2011-10-27 03:02:57.000 |c@tt.com
2 |2011-09-27 03:02:57.000 |d@tt.com
3 |2011-11-01 00:49:12.000 |e@tt.com
3 |2011-11-02 23:16:53.000 |f@tt.com

Result

ID |date | ssemail
1 |2011-10-27 03:02:57.000 |c@tt.com
2 |2011-09-27 03:02:57.000 |d@tt.com
3 |2011-11-02 23:16:53.000 |f@tt.com

when i used above query without ssemail i am gettign correct answer, with ssemail field i am getting wrong result set

pls help


Thanks,
Gangadhara MS
SQL Developer and DBA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-03 : 01:50:04
Check out ROW_NUMBER() function then.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-11-03 : 02:32:50
i am not much into development could somebody pls help me



Thanks,
Gangadhara MS
SQL Developer and DBA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-03 : 02:39:51
Your signature and occupation here on SQLTeam say SQL Developer.

Take a look at example A in BOL for ROW_NUMBER() function: http://msdn.microsoft.com/en-us/library/ms186734.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -