dindanbukuan过滤掉完全重复和不完全重复记录.sql 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --0.备份当前db的dindanbukuan表到dindanbukuan_back中;
  2. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[autoid],[jdd],[financecheck],[discount],[time]
  3. into [db].[dbo].[dindanbukuan_back] from [db].[dbo].[dindanbukuan]
  4. go
  5. --1.将2014和db的dindanbukuan表全部记录插入到#tempall表中;
  6. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck],[discount],[time]
  7. into #tempall from [db].[dbo].[dindanbukuan]
  8. go
  9. insert into #tempall
  10. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck],[discount],[time]
  11. from [2014].[dbo].[dindanbukuan]
  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,item,paytype,jdd,time
  20. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[autoid],[jdd],[financecheck],[discount],[time]
  21. into [db].[dbo].[dindanbukuan#] 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].[dindanbukuan]
  28. use [db]
  29. CREATE TABLE [dbo].[dindanbukuan](
  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](max) NULL,
  36. [kind] [nvarchar](50) NULL,
  37. [ren2] [nvarchar](50) NULL,
  38. [item] [nvarchar](max) NULL,
  39. [paytype] [nvarchar](50) NULL,
  40. [jdd] [nvarchar](50) NULL,
  41. [financecheck] [nvarchar](10) NULL,
  42. [discount] [nvarchar](50) NULL,
  43. [time] [nvarchar](50) NULL
  44. ) ON [PRIMARY]
  45. insert into [db].[dbo].[dindanbukuan]([id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck],[discount],[time])
  46. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck],[discount],[time]
  47. from [db].[dbo].[dindanbukuan#]
  48. drop table [db].[dbo].[dindanbukuan#]
  49. --5.结束;