sql server - How Merge Duplicate Data using MergeTable? -


there duplicate record in table. wrote query find them. result this:

+-----------+-------------+-------------+ |    row    | nationalno  |  client id  | +-----------+-------------+-------------+ |    1      |     10003   |     34      | +-----------+-------------+-------------+ |    2      |     10003   |     75      | +-----------+-------------+-------------+ |    1      |     20023   |     23      | +-----------+-------------+-------------+ |    2      |     20023   |     55      | +-----------+-------------+-------------+ |    3      |     20023   |     12      | +-----------+-------------+-------------+ 

the above result means have 1 client national-no of 10003 whom inserted twice , client national-no of 20023 whom inserted 3 time in client table. not going delete extra. want keep first record active , rest inactive. task save actions history in mergetable. mergetable has 3 columns: clientida, clientidb, date want consider records row of 1 clientida , rest of them clientidb. output needed insert mergetable is:

+-----------+-----------+-------------+ | clientida | clientidb |     date    | +-----------+-----------+-------------+ |    34     |     75    |  2014-06-10 | +-----------+-----------+-------------+ |    23     |     55    |  2014-06-10 | +-----------+-----------+-------------+ |    23     |     12    |  2014-06-10 | +-----------+-----------+-------------+ 

here example how u can do.

you split table 2 (data insert , data not)

and join 2 tables.

declare @duplicates table (row int, nationalno int, clientid int)   insert @duplicates (row, nationalno, clientid) select 1, 10003, 34 insert @duplicates (row, nationalno, clientid) select 2, 10003, 75 insert @duplicates (row, nationalno, clientid) select 1, 20023, 23 insert @duplicates (row, nationalno, clientid) select 2, 20023, 55 insert @duplicates (row, nationalno, clientid) select 3, 20023, 12    ;with clientida (     select row, nationalno, clientid      @duplicates     row = 1 ), clientidb (     select row, nationalno, clientid      @duplicates     row != 1 ) select a.clientid clientida, b.clientid clientidb, getdate() date clientidb b inner join clientida     on a.nationalno = b.nationalno 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -