Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. SELECT num, com FROM TableA
  2. WHERE num, com IN
  3. (
  4. SELECT count(*) AS co FROM TableA WHERE co > 1 GROUP BY num, com
  5. )
  6.  
  7. select ROW_NUMBER() OVER (PARTITION BY [num], com ORDER BY [num], com) as 'RowNumber', t1.*
  8. into ##TableA
  9. from TableA t1
  10.  
  11. delete t1
  12. --select t1.*
  13. from TableA t1
  14. join ##TableC t2
  15. on t2.[num]=t1.[num] and t2.com=t2.com
  16. where t2.RowNumber > 1
  17.  
  18. select * from TableA;
  19. with cte as (
  20. select *, row_number() over (partition by [num], com order by [num], com ) as Picker
  21. from TableA
  22. )
  23. select cte
  24. where Picker > 1
  25.  
  26. Msg 207, Level 16, State 1, Line 7
  27. Invalid column name 'Picker'.
  28. Msg 207, Level 16, State 1, Line 6
  29. Invalid column name 'cte'.
  30.  
  31. select ROW_NUMBER() OVER (PARTITION BY num, com ORDER BY identity_field) as 'RowNumber', t1.*
  32. into ##TableA
  33. from TableA t1
  34.  
  35. --delete t1
  36. select t1.*
  37. from TableA t1
  38. join ##TableA t2
  39. on t2.identity_field=t1.identity_field
  40. where t2.RowNumber > 1
  41.  
  42. --=======================================================
  43. -- delete the duplicate records from table @t
  44. -- keeping a single unit of each
  45. -- marcelo miorelli 24-nov-2014
  46. --=======================================================
  47.  
  48.  
  49. --=======================================================
  50. --create a table variable and insert records in it
  51. -- just for this example
  52. --=======================================================
  53. declare @t table ([num] int, com varbinary(512) )
  54.  
  55. insert into @t select 1, convert(varbinary(512), '6778981' )
  56. insert into @t select 1, convert(varbinary(512), '6778981' )
  57. insert into @t select 1, convert(varbinary(512), '6778982' )
  58. insert into @t select 2, convert(varbinary(512), '6778982' )
  59. insert into @t select 2, convert(varbinary(512), '6778982' )
  60. insert into @t select 3, convert(varbinary(512), '6778982' )
  61. insert into @t select 4, convert(varbinary(512), '6778982' )
  62. insert into @t select 4, convert(varbinary(512), '6778982' )
  63. insert into @t select 4, convert(varbinary(512), '6778982' )
  64. insert into @t select 4, convert(varbinary(512), '6778982' )
  65. insert into @t select 4, convert(varbinary(512), '6778983' )
  66. insert into @t select 5, convert(varbinary(512), '6778983' )
  67. insert into @t select 5, convert(varbinary(512), '6778983' )
  68. insert into @t select 5, convert(varbinary(512), '6778983' )
  69. insert into @t select 5, convert(varbinary(512), '6778983' )
  70. insert into @t select 5, convert(varbinary(512), '6778984' )
  71. insert into @t select 6, convert(varbinary(512), '6778984' )
  72. insert into @t select 6, convert(varbinary(512), '6778984' )
  73. insert into @t select 6, convert(varbinary(512), '6778985' )
  74. insert into @t select 7, convert(varbinary(512), '6778985' )
  75. insert into @t select 7, convert(varbinary(512), '6778985' )
  76. insert into @t select 8, convert(varbinary(512), '6778985' )
  77. insert into @t select 8, convert(varbinary(512), '6778985' )
  78. insert into @t select 9, convert(varbinary(512), '6778985' )
  79. insert into @t select 9, convert(varbinary(512), '6778986' )
  80. insert into @t select 9, convert(varbinary(512), '6778986' )
  81. insert into @t select 9, convert(varbinary(512), '6778986' )
  82. insert into @t select 9, convert(varbinary(512), '6778986' )
  83.  
  84. --=======================================================
  85. -- this is going to show the table before the deletes
  86. --=======================================================
  87. select * from @t
  88.  
  89.  
  90. --=======================================================
  91. -- this deletes the duplicates - leaving only 1 of each
  92. --=======================================================
  93. ; with cte as (
  94. select *
  95. , row_number() over (partition by [num],com order by [num],com ) as Picker
  96. from @t
  97. )
  98. delete cte
  99. where Picker > 1
  100.  
  101.  
  102. --=======================================================
  103. -- this shows the table after the deletion - without the deletes
  104. --=======================================================
  105. select * from @t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement