dindansp过滤掉完全重复和不完全重复记录 - 不含2014.sql 3.9 KB

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