gudingfeiyoung过滤掉完全重复和不完全重复记录.sql 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --0.备份当前db的gudingfeiyong表到gudingfeiyong_back
  2. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto]
  3. into [db].[dbo].[gudingfeiyong_back] from [db].[dbo].[gudingfeiyong]
  4. go
  5. --1.将2014和db的gudingfeiyong表的全部记录插入到#tempall中;
  6. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto]
  7. into #tempall from [db].[dbo].[gudingfeiyong]
  8. go
  9. insert into #tempall
  10. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto]
  11. from [2014].[dbo].[gudingfeiyong]
  12. go
  13. --2.过滤掉#tempall中完全重复的记录到#tempdis中;
  14. --.此步省略……
  15. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  16. select identity(int,1,1) as autoid, * into #temp1 from #tempall
  17. select min(autoid) as autoid into #temp2 from #temp1 group by [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[financecheck2],[financecheck3],[hasphoto]
  18. go
  19. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto]
  20. into [db].[dbo].[gudingfeiyong#] from #temp1 where autoid in(select autoid from #temp2) order by [name]
  21. go
  22. --4.将过滤好的记录重新插入原表;
  23. drop table [db].[dbo].[gudingfeiyong]
  24. go
  25. use db
  26. CREATE TABLE [dbo].[gudingfeiyong](
  27. [id] [int] IDENTITY(1,1) NOT NULL,
  28. [name] [nvarchar](50) NULL,
  29. [money] [nvarchar](50) NULL,
  30. [dat] [nvarchar](50) NULL,
  31. [bz] [nvarchar](max) NULL,
  32. [renyuan1] [nvarchar](50) NULL,
  33. [renyuan2] [nvarchar](50) NULL,
  34. [financecheck] [nvarchar](10) NULL,
  35. [time] [nvarchar](50) NULL,
  36. [photo] [image] NULL,
  37. [financecheck2] [nvarchar](50) NULL,
  38. [financecheck3] [nvarchar](50) NULL,
  39. [hasphoto] [nvarchar](50) NULL
  40. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  41. go
  42. insert into [db].[dbo].[gudingfeiyong]([name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto])
  43. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[photo],[financecheck2],[financecheck3],[hasphoto]
  44. from [db].[dbo].[gudingfeiyong#]
  45. go
  46. drop table #tempall
  47. drop table #temp1
  48. drop table #temp2
  49. drop table [db].[dbo].[gudingfeiyong#]
  50. go
  51. --5.结束