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 2008 Forums
 Transact-SQL (2008)
 How to get the most recent one from a group

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2012-07-01 : 12:05:39
I have three columns

Accountno, Website, Recid

The recid is an incremental unique id. so the higher it is the more recent it is.

Some of the accounts have several website addresses...I just want the most recent one
so my results contain only 1 row per customer account

Hope that makes sense - thanks in advance

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-07-01 : 12:13:41
[code]
select *
from
(
select *, row_no = row_number() over (partition by accountno order by recid desc)
from yourtable
) d
where d.row_no = 1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2012-07-01 : 14:11:00
Thanks very much Khtan.
Hope you have a great day!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-01 : 16:33:48
see scenario 2

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -