tsql - How to remove all the duplicate records except the last occurence in a table -


i have interim table without primary key , identity. need check 1 of columns (branch_ref) value duplicate entries , should mark flag 'd' if branch_ref same more 1 record except last occurrence in table. how can this?

actual data stored in table.actual data stored in table

select branch_name,branch_reference,address_1,zip_cd,null flag_val  branch_master 

as per above table, need flag updated ‘d’ except 6th (brach_reference=9910) , 16th record (branch_reference=99100 , zip_cd=612).

when use row_number function identify duplicates order gets changed.

select branch_name,branch_reference,address_1,zip_cd,flag_val, row_number() over(partition branch_reference order branch_reference) rid           branch_master 

how flag getting updated using below query update flag_val , updating wrong records.

;with cte          (           select branch_name,branch_reference,address_1,zip_cd,flag_val,  row_number() over(partition branch_reference order branch_reference) rid           branch_master           flag_val null         )         update c1 set flag_val = 'd'          cte c1          left outer join (select branch_reference, max(rid) mrid cte group branch_reference) c2              on c1.branch_reference=c2.branch_reference , c1.rid=c2.mrid         c2.branch_reference null 


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -