RedBirdOBX (10/4/2013)
OK. Thanks. I am pulling from a poorly built table and database and inserting into a new database and tables. All works fine except for the source table could and does have duplicate persons. Shouldn't be duplicated but they are. So for example, here is just 5 records (from 7000):[FirstName] | [MiddleName] | [LastName] | [Suffix] | [SSN] | [OwnerNumber]
** GUROWITZ,ANDREW,SCOTT (PRESIDENT) 000000000160
**LANE,WILLIAM, 111111111162
MICHAELS (PRESIDENT)BEYER 222222222163
**KOREN,DANIEL,L 333333333166
**KOREN,DANIEL,L 333333333174
JAMESEKOONS 444444444182
(Yes, there are astericks in name fields. I clean that up later.)
See how "Daniel" is int there twice. Even with the same ssn? I basically need to somehow select the latest, most bottom version of Daniel. The own showing OwnerNumber = 174.
**Those OwnerNumbers are FKs which I'll extract and save later. I cannot just trash them. I'll insert them into a related table once I over come this.
**SSNs are ignored in my INSERT. I come back and grab those later.
So as you can guess, I just used this to grab these records.....
(SELECT [FirstName],[MiddleName],[LastName],[Suffix],[SSN], [OwnersNumber]
FROM ONBOARD.dbo.DealershipOwners WHERE DealershipOwnersID BETWEEN 10 AND 15)
Any idea how I can grab these most bottom version of "Daniel" (and others)?
Well since you still didn't post much of anything useful I can't help you with the code. You can do this with Row_Number() over(Partition by [FirstName], [MiddleName], [LastName], [Suffix], [SSN] order by OwnerNumber desc).
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/