Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT num, com FROM TableA
- WHERE num, com IN
- (
- SELECT count(*) AS co FROM TableA WHERE co > 1 GROUP BY num, com
- )
- select ROW_NUMBER() OVER (PARTITION BY [num], com ORDER BY [num], com) as 'RowNumber', t1.*
- into ##TableA
- from TableA t1
- delete t1
- --select t1.*
- from TableA t1
- join ##TableC t2
- on t2.[num]=t1.[num] and t2.com=t2.com
- where t2.RowNumber > 1
- select * from TableA;
- with cte as (
- select *, row_number() over (partition by [num], com order by [num], com ) as Picker
- from TableA
- )
- select cte
- where Picker > 1
- Msg 207, Level 16, State 1, Line 7
- Invalid column name 'Picker'.
- Msg 207, Level 16, State 1, Line 6
- Invalid column name 'cte'.
- select ROW_NUMBER() OVER (PARTITION BY num, com ORDER BY identity_field) as 'RowNumber', t1.*
- into ##TableA
- from TableA t1
- --delete t1
- select t1.*
- from TableA t1
- join ##TableA t2
- on t2.identity_field=t1.identity_field
- where t2.RowNumber > 1
- --=======================================================
- -- delete the duplicate records from table @t
- -- keeping a single unit of each
- -- marcelo miorelli 24-nov-2014
- --=======================================================
- --=======================================================
- --create a table variable and insert records in it
- -- just for this example
- --=======================================================
- declare @t table ([num] int, com varbinary(512) )
- insert into @t select 1, convert(varbinary(512), '6778981' )
- insert into @t select 1, convert(varbinary(512), '6778981' )
- insert into @t select 1, convert(varbinary(512), '6778982' )
- insert into @t select 2, convert(varbinary(512), '6778982' )
- insert into @t select 2, convert(varbinary(512), '6778982' )
- insert into @t select 3, convert(varbinary(512), '6778982' )
- insert into @t select 4, convert(varbinary(512), '6778982' )
- insert into @t select 4, convert(varbinary(512), '6778982' )
- insert into @t select 4, convert(varbinary(512), '6778982' )
- insert into @t select 4, convert(varbinary(512), '6778982' )
- insert into @t select 4, convert(varbinary(512), '6778983' )
- insert into @t select 5, convert(varbinary(512), '6778983' )
- insert into @t select 5, convert(varbinary(512), '6778983' )
- insert into @t select 5, convert(varbinary(512), '6778983' )
- insert into @t select 5, convert(varbinary(512), '6778983' )
- insert into @t select 5, convert(varbinary(512), '6778984' )
- insert into @t select 6, convert(varbinary(512), '6778984' )
- insert into @t select 6, convert(varbinary(512), '6778984' )
- insert into @t select 6, convert(varbinary(512), '6778985' )
- insert into @t select 7, convert(varbinary(512), '6778985' )
- insert into @t select 7, convert(varbinary(512), '6778985' )
- insert into @t select 8, convert(varbinary(512), '6778985' )
- insert into @t select 8, convert(varbinary(512), '6778985' )
- insert into @t select 9, convert(varbinary(512), '6778985' )
- insert into @t select 9, convert(varbinary(512), '6778986' )
- insert into @t select 9, convert(varbinary(512), '6778986' )
- insert into @t select 9, convert(varbinary(512), '6778986' )
- insert into @t select 9, convert(varbinary(512), '6778986' )
- --=======================================================
- -- this is going to show the table before the deletes
- --=======================================================
- select * from @t
- --=======================================================
- -- this deletes the duplicates - leaving only 1 of each
- --=======================================================
- ; with cte as (
- select *
- , row_number() over (partition by [num],com order by [num],com ) as Picker
- from @t
- )
- delete cte
- where Picker > 1
- --=======================================================
- -- this shows the table after the deletion - without the deletes
- --=======================================================
- select * from @t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement