SQL查询重复处理记录

来源:互联网 发布:血手幽灵数据共享平台 编辑:程序博客网 时间:2024/05/22 21:01
  1. --1、用于查询重复处理记录(如果列没有大小关系时2000用生成自增列和临时表处理,SQL2005用row_number函数处理) 
  2. --> --> (Roy)生成測試數據 
  3. if not object_id('Tempdb..#T'is null 
  4.     drop table #T 
  5. Go 
  6. Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2)) 
  7. Insert #T 
  8. select 1,N'A',N'A1' union all 
  9. select 2,N'A',N'A2' union all 
  10. select 3,N'A',N'A3' union all 
  11. select 4,N'B',N'B1' union all 
  12. select 5,N'B',N'B2' 
  13. Go 
  14. --I、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2 
  15. 方法1: 
  16. Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID <a.ID) 
  17. 方法2: 
  18. select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID 
  19. 方法3: 
  20. select * from #T a where ID=(select min(ID) from #T where Name=a.Name) 
  21. 方法4: 
  22. select a.* from #T a join #T b on a.Name=b.Name and a.ID>=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 
  23. 方法5: 
  24. select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name) 
  25. 方法6: 
  26. select * from #T a where (select count(1) from #T where Name=a.Name and ID <a.ID)=0 
  27. 方法7: 
  28. select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID) 
  29. 方法8: 
  30. select * from #T a where ID!>all(select ID from #T where Name=a.Name) 
  31. 方法9(注:ID为唯一时可用): 
  32. select * from #T a where ID in(select min(ID) from #T group by Name) 
  33. --SQL2005: 
  34. 方法10: 
  35. select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID 
  36. 方法11: 
  37. select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1 
  38. 生成结果: 
  39. /* 
  40. ID          Name Memo 
  41. ----------- ---- ---- 
  42. 1          A    A1 
  43. 4          B    B1 
  44. (2 行受影响) 
  45. */ 
  46. --II、Name相同ID最大的记录,与min相反: 
  47. 方法1: 
  48. Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID) 
  49. 方法2: 
  50. select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID 
  51. 方法3: 
  52. select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID 
  53. 方法4: 
  54. select a.* from #T a join #T b on a.Name=b.Name and a.ID <=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 
  55. 方法5: 
  56. select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name) 
  57. 方法6: 
  58. select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0 
  59. 方法7: 
  60. select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc) 
  61. 方法8: 
  62. select * from #T a where ID! <all(select ID from #T where Name=a.Name) 
  63. 方法9(注:ID为唯一时可用): 
  64. select * from #T a where ID in(select max(ID) from #T group by Name) 
  65. --SQL2005: 
  66. 方法10: 
  67. select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID 
  68. 方法11: 
  69. select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID desc) as MinID from #T a)T where MinID=1 
  70. 生成结果2: 
  71. /* 
  72. ID          Name Memo 
  73. ----------- ---- ---- 
  74. 3          A    A3 
  75. 5          B    B2 
  76. (2 行受影响) 
  77. */ 
  78. --2、删除重复记录有大小关系时,保留大或小其中一个记录 
  79. --> --> (Roy)生成測試數據 
  80. if not object_id('Tempdb..#T'is null 
  81.     drop table #T 
  82. Go 
  83. Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2)) 
  84. Insert #T 
  85. select 1,N'A',N'A1' union all 
  86. select 2,N'A',N'A2' union all 
  87. select 3,N'A',N'A3' union all 
  88. select 4,N'B',N'B1' union all 
  89. select 5,N'B',N'B2' 
  90. Go 
  91. --I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条 
  92. 方法1: 
  93. delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID <a.ID) 
  94. 方法2: 
  95. delete a  from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null 
  96. 方法3: 
  97. delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name) 
  98. 方法4(注:ID为唯一时可用): 
  99. delete a from #T a where ID not in(select min(ID)from #T group by Name) 
  100. 方法5: 
  101. delete a from #T a where (select count(1) from #T where Name=a.Name and ID <a.ID)>0 
  102. 方法6: 
  103. delete a from #T a where ID <>(select top 1 ID from #T where Name=a.name order by ID) 
  104. 方法7: 
  105. delete a from #T a where ID>any(select ID from #T where Name=a.Name) 
  106. select * from #T 
  107. 生成结果: 
  108. /* 
  109. ID          Name Memo 
  110. ----------- ---- ---- 
  111. 1          A    A1 
  112. 4          B    B1 
  113. (2 行受影响) 
  114. */ 
  115. --II、Name相同ID保留最大的一条记录: 
  116. 方法1: 
  117. delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID>a.ID) 
  118. 方法2: 
  119. delete a  from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null 
  120. 方法3: 
  121. delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name) 
  122. 方法4(注:ID为唯一时可用): 
  123. delete a from #T a where ID not in(select max(ID)from #T group by Name) 
  124. 方法5: 
  125. delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0 
  126. 方法6: 
  127. delete a from #T a where ID <>(select top 1 ID from #T where Name=a.name order by ID desc) 
  128. 方法7: 
  129. delete a from #T a where ID <any(select ID from #T where Name=a.Name) 
  130. select * from #T 
  131. /* 
  132. ID          Name Memo 
  133. ----------- ---- ---- 
  134. 3          A    A3 
  135. 5          B    B2 
  136. (2 行受影响) 
  137. */ 
  138. --3、删除重复记录没有大小关系时,处理重复值 
  139. --> --> (Roy)生成測試數據 
  140. if not object_id('Tempdb..#T'is null 
  141.     drop table #T 
  142. Go 
  143. Create table #T([Num] int,[Name] nvarchar(1)) 
  144. Insert #T 
  145. select 1,N'A' union all 
  146. select 1,N'A' union all 
  147. select 1,N'A' union all 
  148. select 2,N'B' union all 
  149. select 2,N'B' 
  150. Go 
  151. 方法1: 
  152. if object_id('Tempdb..#'is not null 
  153.     drop table # 
  154. Select distinct * into # from #T--排除重复记录结果集生成临时表# 
  155. truncate table #T--清空表 
  156. insert #T select * from #    --把临时表#插入到表#T中 
  157. --查看结果 
  158. select * from #T 
  159. /* 
  160. Num        Name 
  161. ----------- ---- 
  162. 1          A 
  163. 2          B 
  164. (2 行受影响) 
  165. */ 
  166. --重新执行测试数据后用方法2 
  167. 方法2: 
  168. alter table #T add ID int identity--新增标识列 
  169. go 
  170. delete a from  #T a where  exists(select 1 from #T where Num=a.Num and Name=a.Name and ID>a.ID)--只保留一条记录 
  171. go 
  172. alter table #T drop column ID--删除标识列 
  173. --查看结果 
  174. select * from #T 
  175. /* 
  176. Num        Name 
  177. ----------- ---- 
  178. 1          A 
  179. 2          B 
  180. (2 行受影响) 
  181. */ 
  182. --重新执行测试数据后用方法3 
  183. 方法3: 
  184. declare Roy_Cursor cursor local for 
  185. select count(1)-1,Num,Name from #T group by Num,Name having count(1)>1 
  186. declare @con int,@Num int,@Name nvarchar(1) 
  187. open Roy_Cursor 
  188. fetch next from Roy_Cursor into @con,@Num,@Name 
  189. while @@Fetch_status=0 
  190. begin 
  191.     set rowcount @con; 
  192.     delete #T where Num=@Num and Name=@Name 
  193.     set rowcount 0; 
  194.     fetch next from Roy_Cursor into @con,@Num,@Name 
  195. end 
  196. close Roy_Cursor 
  197. deallocate Roy_Cursor 
  198. --查看结果 
  199. select * from #T 
  200. /* 
  201. Num        Name 
  202. ----------- ---- 
  203. 1          A 
  204. 2          B 
  205. (2 行受影响) 
  206. --------------------------
  207. --按某一字段分组取最大(小)值所在行的数据
  208. (爱新觉罗.毓华 2007-10-23于浙江杭州)
  209. /*
  210. 数据如下:
  211. name val memo
  212. a    2   a2(a的第二个值)
  213. a    1   a1--a的第一个值
  214. a    3   a3:a的第三个值
  215. b    1   b1--b的第一个值
  216. b    3   b3:b的第三个值
  217. b    2   b2b2b2b2
  218. b    4   b4b4
  219. b    5   b5b5b5b5b5
  220. */
  221. --创建表并插入数据:
  222. create table tb(name varchar(10),val int,memo varchar(20))
  223. insert into tb values('a',    2,   'a2(a的第二个值)')
  224. insert into tb values('a',    1,   'a1--a的第一个值')
  225. insert into tb values('a',    3,   'a3:a的第三个值')
  226. insert into tb values('b',    1,   'b1--b的第一个值')
  227. insert into tb values('b',    3,   'b3:b的第三个值')
  228. insert into tb values('b',    2,   'b2b2b2b2')
  229. insert into tb values('b',    4,   'b4b4')
  230. insert into tb values('b',    5,   'b5b5b5b5b5')
  231. go
  232. --一、按name分组取val最大的值所在行的数据。
  233. --方法1:
  234. select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
  235. --方法2:
  236. select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
  237. --方法3:
  238. select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
  239. --方法4:
  240. select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
  241. --方法5
  242. select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
  243. /*
  244. name       val         memo                 
  245. ---------- ----------- -------------------- 
  246. a          3           a3:a的第三个值
  247. b          5           b5b5b5b5b5
  248. */
  249. --二、按name分组取val最小的值所在行的数据。
  250. --方法1:
  251. select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
  252. --方法2:
  253. select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
  254. --方法3:
  255. select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
  256. --方法4:
  257. select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
  258. --方法5
  259. select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
  260. /*
  261. name       val         memo                 
  262. ---------- ----------- -------------------- 
  263. a          1           a1--a的第一个值
  264. b          1           b1--b的第一个值
  265. */
  266. --三、按name分组取第一次出现的行所在的数据。
  267. select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
  268. /*
  269. name       val         memo                 
  270. ---------- ----------- -------------------- 
  271. a          2           a2(a的第二个值)
  272. b          1           b1--b的第一个值
  273. */
  274. --四、按name分组随机取一条数据。
  275. select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
  276. /*
  277. name       val         memo                 
  278. ---------- ----------- -------------------- 
  279. a          1           a1--a的第一个值
  280. b          5           b5b5b5b5b5
  281. */
  282. --五、按name分组取最小的两个(N个)val
  283. select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
  284. select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
  285. select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
  286. /*
  287. name       val         memo                 
  288. ---------- ----------- -------------------- 
  289. a          1           a1--a的第一个值
  290. a          2           a2(a的第二个值)
  291. b          1           b1--b的第一个值
  292. b          2           b2b2b2b2
  293. */
  294. --六、按name分组取最大的两个(N个)val
  295. select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
  296. select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
  297. select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
  298. /*
  299. name       val         memo                 
  300. ---------- ----------- -------------------- 
  301. a          2           a2(a的第二个值)
  302. a          3           a3:a的第三个值
  303. b          4           b4b4
  304. b          5           b5b5b5b5b5
  305. */
  306. --七,如果整行数据有重复,所有的列都相同。
  307. /*
  308. 数据如下:
  309. name val memo
  310. a    2   a2(a的第二个值)
  311. a    1   a1--a的第一个值
  312. a    1   a1--a的第一个值
  313. a    3   a3:a的第三个值
  314. a    3   a3:a的第三个值
  315. b    1   b1--b的第一个值
  316. b    3   b3:b的第三个值
  317. b    2   b2b2b2b2
  318. b    4   b4b4
  319. b    5   b5b5b5b5b5
  320. */
  321. --在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
  322. --创建表并插入数据:
  323. create table tb(name varchar(10),val int,memo varchar(20))
  324. insert into tb values('a',    2,   'a2(a的第二个值)')
  325. insert into tb values('a',    1,   'a1--a的第一个值')
  326. insert into tb values('a',    1,   'a1--a的第一个值')
  327. insert into tb values('a',    3,   'a3:a的第三个值')
  328. insert into tb values('a',    3,   'a3:a的第三个值')
  329. insert into tb values('b',    1,   'b1--b的第一个值')
  330. insert into tb values('b',    3,   'b3:b的第三个值')
  331. insert into tb values('b',    2,   'b2b2b2b2')
  332. insert into tb values('b',    4,   'b4b4')
  333. insert into tb values('b',    5,   'b5b5b5b5b5')
  334. go
  335. select * , px = identity(int,1,1) into tmp from tb
  336. select m.name,m.val,m.memo from
  337. (
  338.   select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
  339. ) m where px = (select min(px) from
  340. (
  341.   select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
  342. ) n where n.name = m.name)
  343. drop table tb,tmp
  344. /*
  345. name       val         memo
  346. ---------- ----------- --------------------
  347. a          1           a1--a的第一个值
  348. b          1           b1--b的第一个值
  349. (2 行受影响)
  350. */
  351. --在sql server 2005中可以使用row_number函数,不需要使用临时表。
  352. --创建表并插入数据:
  353. create table tb(name varchar(10),val int,memo varchar(20))
  354. insert into tb values('a',    2,   'a2(a的第二个值)')
  355. insert into tb values('a',    1,   'a1--a的第一个值')
  356. insert into tb values('a',    1,   'a1--a的第一个值')
  357. insert into tb values('a',    3,   'a3:a的第三个值')
  358. insert into tb values('a',    3,   'a3:a的第三个值')
  359. insert into tb values('b',    1,   'b1--b的第一个值')
  360. insert into tb values('b',    3,   'b3:b的第三个值')
  361. insert into tb values('b',    2,   'b2b2b2b2')
  362. insert into tb values('b',    4,   'b4b4')
  363. insert into tb values('b',    5,   'b5b5b5b5b5')
  364. go
  365. select m.name,m.val,m.memo from
  366. (
  367.   select * , px = row_number() over(order by name , val) from tb
  368. ) m where px = (select min(px) from
  369. (
  370.   select * , px = row_number() over(order by name , val) from tb
  371. ) n where n.name = m.name)
  372. drop table tb
  373. /*
  374. name       val         memo
  375. ---------- ----------- --------------------
  376. a          1           a1--a的第一个值
  377. b          1           b1--b的第一个值
  378. (2 行受影响)
  379. */
  380. select * from @T t where userid=(select min(userid) from @T where left(loginname,3)=left(t.loginname,3))
  381. ----------------------------------------------------
  382. --------------删除所有重复记录 
  383. delete a where col1 in (select col1 from a group by col001 having count(*)>1) 
  384. ------------------全部重复 保留一条数据, 
  385. select * into #b from a ---备份 
  386. truncate table #b 
  387. insert into a 
  388. select disitnct  * from #b 
  389. ---------------某个字段重复,只保留一条数据库(用游标) 
  390. -------------------------用游标过滤重复记录 
  391. declare @aa varchar(60) 
  392. declare www cursor for 
  393. select h_name from join_product_ty_bf 
  394. open www 
  395. fetch next from www into @aa 
  396. while(@@fetch_status=0) 
  397. begin 
  398. fetch next from www into @aa 
  399. if exists 
  400. (select h_name from join_product_ty_bf where h_name like @aa group by h_name  having count(*)>1) 
  401. delete join_product_ty_bf where current of www 
  402. end
原创粉丝点击