client过滤掉完全重复和不完全重复记录 - 不包含2014.sql 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. --2.过滤掉完全重复的记录;
  50. --select distinct * into #tempdis from #tempall
  51. --go
  52. --drop table #tempall
  53. --go
  54. --此步省略……
  55. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  56. select identity(int,1,1) as autoid, * into #temp1 from #tempall
  57. go
  58. select min(autoid) as autoid into #temp2 from #temp1 group by id
  59. go
  60. select [id]
  61. ,[name1]
  62. ,[name2]
  63. ,[phone1]
  64. ,[phone2]
  65. ,[qq1]
  66. ,[qq2]
  67. ,[addr1]
  68. ,[addr2]
  69. ,[occupation1]
  70. ,[occupation2]
  71. ,[birthday1]
  72. ,[birthday2]
  73. ,[time3]
  74. ,[area]
  75. ,[area2]
  76. ,[check1]
  77. ,[check2]
  78. ,[check3]
  79. ,[sex]
  80. ,[zodiac]
  81. ,[photo]
  82. into [db].[dbo].[client#] from #temp1 where autoid in(select autoid from #temp2) order by id
  83. go
  84. --4.将过滤好的记录重新插入原表;
  85. drop table #tempall
  86. drop table #temp1
  87. drop table #temp2
  88. truncate table [db].[dbo].[client]
  89. go
  90. insert into [db].[dbo].[client]([id]
  91. ,[name1]
  92. ,[name2]
  93. ,[phone1]
  94. ,[phone2]
  95. ,[qq1]
  96. ,[qq2]
  97. ,[addr1]
  98. ,[addr2]
  99. ,[occupation1]
  100. ,[occupation2]
  101. ,[birthday1]
  102. ,[birthday2]
  103. ,[time3]
  104. ,[area]
  105. ,[area2]
  106. ,[check1]
  107. ,[check2]
  108. ,[check3]
  109. ,[sex]
  110. ,[zodiac]
  111. ,[photo])
  112. select [id]
  113. ,[name1]
  114. ,[name2]
  115. ,[phone1]
  116. ,[phone2]
  117. ,[qq1]
  118. ,[qq2]
  119. ,[addr1]
  120. ,[addr2]
  121. ,[occupation1]
  122. ,[occupation2]
  123. ,[birthday1]
  124. ,[birthday2]
  125. ,[time3]
  126. ,[area]
  127. ,[area2]
  128. ,[check1]
  129. ,[check2]
  130. ,[check3]
  131. ,[sex]
  132. ,[zodiac]
  133. ,[photo] from [db].[dbo].[client#]
  134. go
  135. drop table [db].[dbo].[client#]
  136. go
  137. --5.结束;