dindanbukuan2过滤掉完全重复和不完全重复记录.sql 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. --0.备份当前db的dindanbukuan2表到dindanbukuan2_back中;
  2. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  3. into [db].[dbo].[dindanbukuan2_back] from [db].[dbo].[dindanbukuan2]
  4. go
  5. --1.将2014和db的dindanbukuan2表全部记录插入到#tempall表中;
  6. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  7. into #tempall from [db].[dbo].[dindanbukuan2]
  8. go
  9. insert into #tempall
  10. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  11. from [2014].[dbo].[dindanbukuan2]
  12. go
  13. --2.过滤掉完全重复的记录;
  14. select distinct * into #tempdis from #tempall
  15. go
  16. drop table #tempall
  17. --3.获取指定字段的不重复记录,过滤掉不完全重复的记录;
  18. select identity(int,1,1) as autoid, * into #temp1 from #tempdis
  19. select min(autoid) as autoid into #temp2 from #temp1 group by [id],[money],[ren],[date],[bz],[kind],[ren2]
  20. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  21. into [db].[dbo].[dindanbukuan2#] from #temp1 where autoid in(select autoid from #temp2) order by id
  22. go
  23. --4.将过滤好的记录重新插入原表;
  24. drop table #tempdis
  25. drop table #temp1
  26. drop table #temp2
  27. drop table [db].[dbo].[dindanbukuan2]
  28. use [db]
  29. CREATE TABLE [dbo].[dindanbukuan2](
  30. [autoid] [int] IDENTITY(1,1) NOT NULL,
  31. [id] [nvarchar](50) NULL,
  32. [money] [nvarchar](50) NULL,
  33. [ren] [nvarchar](50) NULL,
  34. [date] [nvarchar](50) NULL,
  35. [bz] [nvarchar](500) NULL,
  36. [kind] [nvarchar](50) NULL,
  37. [ren2] [nvarchar](50) NULL
  38. ) ON [PRIMARY]
  39. insert into [db].[dbo].[dindanbukuan2]([id],[money],[ren],[date],[bz],[kind],[ren2])
  40. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  41. from [db].[dbo].[dindanbukuan2#]
  42. drop table [db].[dbo].[dindanbukuan2#]
  43. --5.结束;