singleincome过滤掉完全重复和不完全重复记录.sql 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. 申明:此脚本禁用,因为[singleincomemoney]与[singleincome]的id必须相同的,即singleincome的id相当于是[singleincomemoney]的外键作用
  3. */
  4. --0.备份当前db的singleincome表到singleincome_back
  5. select [name]
  6. ,[money]
  7. ,[dat]
  8. ,[bz]
  9. ,[renyuan1]
  10. ,[renyuan2]
  11. ,[renyuan3]
  12. ,[sale2type]
  13. ,[paytype]
  14. ,[phone]
  15. ,[payed]
  16. ,[balance]
  17. ,[pinyin]
  18. ,[financecheck]
  19. ,[count]
  20. ,[time]
  21. into [db].[dbo].[singleincome_back] from [db].[dbo].[singleincome]
  22. go
  23. --1.将2014和db的dindnasp3表的全部记录插入到#tempall中;
  24. select [name]
  25. ,[money]
  26. ,[dat]
  27. ,[bz]
  28. ,[renyuan1]
  29. ,[renyuan2]
  30. ,[renyuan3]
  31. ,[sale2type]
  32. ,[paytype]
  33. ,[phone]
  34. ,[payed]
  35. ,[balance]
  36. ,[pinyin]
  37. ,[financecheck]
  38. ,[count]
  39. ,[time]
  40. into #tempall from [db].[dbo].[singleincome]
  41. go
  42. insert into #tempall
  43. select [name]
  44. ,[money]
  45. ,[dat]
  46. ,[bz]
  47. ,[renyuan1]
  48. ,[renyuan2]
  49. ,[renyuan3]
  50. ,[sale2type]
  51. ,[paytype]
  52. ,[phone]
  53. ,[payed]
  54. ,[balance]
  55. ,[pinyin]
  56. ,[financecheck]
  57. ,[count]
  58. ,[time] from [2014].[dbo].[singleincome]
  59. go
  60. --2.过滤掉#tempall中完全重复的记录到#tempdis中;
  61. select distinct * into #tempdis from #tempall
  62. go
  63. drop table #tempall
  64. go
  65. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  66. select identity(int,1,1) as autoid, * into #temp1 from #tempdis
  67. select min(autoid) as autoid into #temp2 from #temp1 group by [name],[money],[dat],[bz],[renyuan1],[renyuan2],[renyuan3]
  68. ,[sale2type],[paytype],[phone],[payed],[balance],[pinyin],[financecheck],[count],[time]
  69. select [name]
  70. ,[money]
  71. ,[dat]
  72. ,[bz]
  73. ,[renyuan1]
  74. ,[renyuan2]
  75. ,[renyuan3]
  76. ,[sale2type]
  77. ,[paytype]
  78. ,[phone]
  79. ,[payed]
  80. ,[balance]
  81. ,[pinyin]
  82. ,[financecheck]
  83. ,[count]
  84. ,[time]
  85. into [db].[dbo].[singleincome#] from #temp1 where autoid in(select autoid from #temp2) order by [name]
  86. go
  87. --4.将过滤好的记录重新插入原表;
  88. drop table [db].[dbo].[singleincome]
  89. go
  90. use db
  91. CREATE TABLE [dbo].[singleincome](
  92. [id] [int] IDENTITY(1,1) NOT NULL,
  93. [name] [nvarchar](50) NULL,
  94. [money] [nvarchar](50) NULL,
  95. [dat] [nvarchar](50) NULL,
  96. [bz] [nvarchar](max) NULL,
  97. [renyuan1] [nvarchar](50) NULL,
  98. [renyuan2] [nvarchar](50) NULL,
  99. [renyuan3] [nvarchar](50) NULL,
  100. [sale2type] [nvarchar](50) NULL,
  101. [paytype] [nvarchar](50) NULL,
  102. [phone] [nvarchar](50) NULL,
  103. [payed] [nvarchar](50) NULL,
  104. [balance] [nvarchar](50) NULL,
  105. [pinyin] [nvarchar](50) NULL,
  106. [financecheck] [nvarchar](10) NULL,
  107. [count] [nvarchar](50) NULL,
  108. [time] [nvarchar](50) NULL
  109. ) ON [PRIMARY]
  110. go
  111. insert into [db].[dbo].[singleincome]([name]
  112. ,[money]
  113. ,[dat]
  114. ,[bz]
  115. ,[renyuan1]
  116. ,[renyuan2]
  117. ,[renyuan3]
  118. ,[sale2type]
  119. ,[paytype]
  120. ,[phone]
  121. ,[payed]
  122. ,[balance]
  123. ,[pinyin]
  124. ,[financecheck]
  125. ,[count]
  126. ,[time])
  127. select [name]
  128. ,[money]
  129. ,[dat]
  130. ,[bz]
  131. ,[renyuan1]
  132. ,[renyuan2]
  133. ,[renyuan3]
  134. ,[sale2type]
  135. ,[paytype]
  136. ,[phone]
  137. ,[payed]
  138. ,[balance]
  139. ,[pinyin]
  140. ,[financecheck]
  141. ,[count]
  142. ,[time] from [db].[dbo].[singleincome#]
  143. go
  144. drop table #tempdis
  145. drop table #temp1
  146. drop table #temp2
  147. drop table [db].[dbo].[singleincome#]
  148. go
  149. --5.结束