dindansp过滤掉完全重复和不完全重复记录.sql 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --0.备份当前db的dindansp表到dindansp_back
  2. select [id],[spid],[shuliang],[kind],[price]
  3. ,[name],[no],[status1],[status2],[status3]
  4. ,[status4],[date1],[date2],[date3],[date4],[name1]
  5. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  6. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  7. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  8. into [db].[dbo].[dindansp_back] from [db].[dbo].[dindansp]
  9. go
  10. --1.将2014和db的dindnasp表的全部记录插入到#tempall中;
  11. select [id],[spid],[shuliang],[kind],[price]
  12. ,[name],[no],[status1],[status2],[status3]
  13. ,[status4],[date1],[date2],[date3],[date4],[name1]
  14. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  15. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  16. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  17. into #tempall from [db].[dbo].[dindansp]
  18. go
  19. insert into #tempall
  20. select [id],[spid],[shuliang],[kind],[price]
  21. ,[name],[no],[status1],[status2],[status3]
  22. ,[status4],[date1],[date2],[date3],[date4],[name1]
  23. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  24. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  25. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  26. from [2014].[dbo].[dindansp]
  27. go
  28. --2.过滤掉#tempall中完全重复的记录到#tempdis中;
  29. select distinct * into #tempdis from #tempall
  30. go
  31. drop table #tempall
  32. go
  33. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  34. select identity(int,1,1) as autoid, * into #temp1 from #tempdis
  35. select min(autoid) as autoid into #temp2 from #temp1 group by [id],[spid],[name],[no]
  36. select [id],[spid],[shuliang],[kind],[price]
  37. ,[name],[no],[status1],[status2],[status3]
  38. ,[status4],[date1],[date2],[date3],[date4],[name1]
  39. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  40. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  41. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  42. into [db].[dbo].[dindansp#] from #temp1 where autoid in(select autoid from #temp2) order by id
  43. go
  44. --4.将过滤好的记录重新插入原表;
  45. drop table [db].[dbo].[dindansp]
  46. go
  47. use [db]
  48. CREATE TABLE [dbo].[dindansp](
  49. [id] [nvarchar](50) NULL,
  50. [spid] [nvarchar](50) NULL,
  51. [shuliang] [nvarchar](120) NULL,
  52. [kind] [nvarchar](10) NULL,
  53. [price] [nvarchar](50) NULL,
  54. [name] [nvarchar](50) NULL,
  55. [no] [nvarchar](max) NULL,
  56. [status1] [nvarchar](50) NULL,
  57. [status2] [nvarchar](50) NULL,
  58. [status3] [nvarchar](50) NULL,
  59. [status4] [nvarchar](50) NULL,
  60. [autoid] [int] IDENTITY(1,1) NOT NULL,
  61. [date1] [nvarchar](50) NULL,
  62. [date2] [nvarchar](50) NULL,
  63. [date3] [nvarchar](50) NULL,
  64. [date4] [nvarchar](50) NULL,
  65. [name1] [nvarchar](50) NULL,
  66. [name2] [nvarchar](50) NULL,
  67. [name3] [nvarchar](50) NULL,
  68. [name4] [nvarchar](50) NULL,
  69. [hqdate] [nvarchar](50) NULL,
  70. [hqtime] [nvarchar](50) NULL,
  71. [urgent] [nvarchar](50) NULL,
  72. [taketime] [nvarchar](50) NULL,
  73. [no2] [nvarchar](max) NULL,
  74. [growthtxname] [nvarchar](50) NULL,
  75. [status5] [nvarchar](50) NULL,
  76. [status6] [nvarchar](50) NULL,
  77. [date5] [nvarchar](50) NULL,
  78. [date6] [nvarchar](50) NULL,
  79. [name5] [nvarchar](50) NULL,
  80. [name6] [nvarchar](50) NULL,
  81. [ps] [nvarchar](50) NULL,
  82. [zs] [nvarchar](50) NULL,
  83. [ren1] [nvarchar](120) NULL,
  84. [ren2] [nvarchar](120) NULL,
  85. [hqstatus] [nvarchar](50) NULL,
  86. [bz] [nvarchar](200) NULL
  87. ) ON [PRIMARY]
  88. go
  89. insert into [db].[dbo].[dindansp]([id],[spid],[shuliang],[kind],[price]
  90. ,[name],[no],[status1],[status2],[status3]
  91. ,[status4],[date1],[date2],[date3],[date4],[name1]
  92. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  93. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  94. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz] )
  95. select [id],[spid],[shuliang],[kind],[price]
  96. ,[name],[no],[status1],[status2],[status3]
  97. ,[status4],[date1],[date2],[date3],[date4],[name1]
  98. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  99. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  100. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz] from [db].[dbo].[dindansp#]
  101. go
  102. drop table #tempdis
  103. drop table #temp1
  104. drop table #temp2
  105. drop table [db].[dbo].[dindansp#]
  106. go
  107. --5.结束