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.
| Author |
Topic |
|
divan
Posting Yak Master
153 Posts |
Posted - 2012-08-31 : 10:04:01
|
| I have the following fields in a tablenumber, client_number,first_name,last_name,amountexample of dataTX 11240 13 NULL Sad Clinic 313307TX 11240 13 NULL Sad Clinic 341523TX 10762 17 NULL North 22212TX 11240 69 NULL ler Clinic 313307TX 11240 69 NULL ler Clinic 341523TX 10636 83 Bernardino Abaya 12538TX 10184 92 iya Ahm 7603TX 11920 139 Elzbieta Baranowska-Daca 0TX 11540 140 ernard Barrett 17649I am using the folling script to remove the duplicatesDELETEFROM #TEMP1WHERE POLICY_NUMBER NOT IN(SELECT MAX(POLICY_NUMBER)FROM #TEMP1GROUP BYCLIENT_NUMBER,LAST_NAME)SELECT * FROM #TEMP1 expecting the followingTX 11240 13 NULL Sad Clinic 341523TX 10762 17 NULL North 22212TX 11240 69 NULL ler Clinic 341523TX 10636 83 Bernardino Abaya 12538TX 10184 92 iya Ahm 7603TX 11920 139 Elzbieta Baranowska-Daca 0TX 11540 140 ernard Barrett 17649but instead it is not removing anything...Any suggestions??? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-31 : 10:06:52
|
| so you always want to retain record with largest amount for each group?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
divan
Posting Yak Master
153 Posts |
Posted - 2012-08-31 : 10:11:07
|
| Yes..or just removing the duplicate it really does not matter... |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-08-31 : 10:15:20
|
| declare @t table (i int, j int)insert @t select 1,1insert @t select 1,1insert @t select 2,1insert @t select 2,1insert @t select 2,1insert @t select 3,1; with cte as(select i, seq = ROW_NUMBER() over (partition by i order by j) from @t)delete ctewhere seq <> 1select * from @t==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-31 : 10:16:43
|
| [code]DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY number, client_number ORDER BY amount DESC) AS Seq,*FROM table)tWHERE Seq>1[/code]suggest you to first put a select and check the records and then do delete once staisfied with result------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|