client过滤掉完全重复和不完全重复记录.sql 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. --0.备份当前db的client表到client_back中;
  2. select [id]
  3. ,[name1]
  4. ,[name2]
  5. ,[phone1]
  6. ,[phone2]
  7. ,[qq1]
  8. ,[qq2]
  9. ,[addr1]
  10. ,[addr2]
  11. ,[occupation1]
  12. ,[occupation2]
  13. ,[birthday1]
  14. ,[birthday2]
  15. ,[time3]
  16. ,[area]
  17. ,[area2]
  18. ,[check1]
  19. ,[check2]
  20. ,[check3]
  21. ,[sex]
  22. ,[zodiac]
  23. ,[photo] into [db].[dbo].[client_back] from [db].[dbo].[client]
  24. go
  25. --1.将db的client表全部记录插入到#tempall表中;
  26. select [id]
  27. ,[name1]
  28. ,[name2]
  29. ,[phone1]
  30. ,[phone2]
  31. ,[qq1]
  32. ,[qq2]
  33. ,[addr1]
  34. ,[addr2]
  35. ,[occupation1]
  36. ,[occupation2]
  37. ,[birthday1]
  38. ,[birthday2]
  39. ,[time3]
  40. ,[area]
  41. ,[area2]
  42. ,[check1]
  43. ,[check2]
  44. ,[check3]
  45. ,[sex]
  46. ,[zodiac]
  47. ,[photo] into #tempall from [db].[dbo].[client]
  48. go
  49. insert into #tempall select [id]
  50. ,[name1]
  51. ,[name2]
  52. ,[phone1]
  53. ,[phone2]
  54. ,[qq1]
  55. ,[qq2]
  56. ,[addr1]
  57. ,[addr2]
  58. ,[occupation1]
  59. ,[occupation2]
  60. ,[birthday1]
  61. ,[birthday2]
  62. ,[time3]
  63. ,[area]
  64. ,[area2]
  65. ,[check1]
  66. ,[check2]
  67. ,[check3]
  68. ,[sex]
  69. ,[zodiac]
  70. ,[photo] from [2014].[dbo].[client]
  71. go
  72. --2.过滤掉完全重复的记录;
  73. --select distinct * into #tempdis from #tempall
  74. --go
  75. --drop table #tempall
  76. --go
  77. --此步省略……
  78. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  79. select identity(int,1,1) as autoid, * into #temp1 from #tempall
  80. go
  81. select min(autoid) as autoid into #temp2 from #temp1 group by id
  82. go
  83. select [id]
  84. ,[name1]
  85. ,[name2]
  86. ,[phone1]
  87. ,[phone2]
  88. ,[qq1]
  89. ,[qq2]
  90. ,[addr1]
  91. ,[addr2]
  92. ,[occupation1]
  93. ,[occupation2]
  94. ,[birthday1]
  95. ,[birthday2]
  96. ,[time3]
  97. ,[area]
  98. ,[area2]
  99. ,[check1]
  100. ,[check2]
  101. ,[check3]
  102. ,[sex]
  103. ,[zodiac]
  104. ,[photo]
  105. into [db].[dbo].[client#] from #temp1 where autoid in(select autoid from #temp2) order by id
  106. go
  107. --4.将过滤好的记录重新插入原表;
  108. drop table #tempall
  109. drop table #temp1
  110. drop table #temp2
  111. truncate table [db].[dbo].[client]
  112. go
  113. insert into [db].[dbo].[client]([id]
  114. ,[name1]
  115. ,[name2]
  116. ,[phone1]
  117. ,[phone2]
  118. ,[qq1]
  119. ,[qq2]
  120. ,[addr1]
  121. ,[addr2]
  122. ,[occupation1]
  123. ,[occupation2]
  124. ,[birthday1]
  125. ,[birthday2]
  126. ,[time3]
  127. ,[area]
  128. ,[area2]
  129. ,[check1]
  130. ,[check2]
  131. ,[check3]
  132. ,[sex]
  133. ,[zodiac]
  134. ,[photo])
  135. select [id]
  136. ,[name1]
  137. ,[name2]
  138. ,[phone1]
  139. ,[phone2]
  140. ,[qq1]
  141. ,[qq2]
  142. ,[addr1]
  143. ,[addr2]
  144. ,[occupation1]
  145. ,[occupation2]
  146. ,[birthday1]
  147. ,[birthday2]
  148. ,[time3]
  149. ,[area]
  150. ,[area2]
  151. ,[check1]
  152. ,[check2]
  153. ,[check3]
  154. ,[sex]
  155. ,[zodiac]
  156. ,[photo] from [db].[dbo].[client#]
  157. go
  158. drop table [db].[dbo].[client#]
  159. go
  160. --5.结束;