使用PostgreSQL查找重复的行 [英] Find duplicate rows with PostgreSQL

查看:425
本文介绍了使用PostgreSQL查找重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一张包含以下列的照片表。

We have a table of photos with the following columns.

id, merchant_id, url 

此表包含组合merchant_id,url的重复值。因此有可能一行出现更多次。

this table contains duplicate values for the combination merchant_id, url. so it's possible that one row appears more several times.

234 some_merchant  http://www.some-image-url.com/abscde1213
235 some_merchant  http://www.some-image-url.com/abscde1213
236 some_merchant  http://www.some-image-url.com/abscde1213

删除这些重复项的最佳方法是什么?
(我使用PostgreSQL 9.2和Rails 3。)

What is the best way to delete those duplications? (I use PostgreSQL 9.2 and Rails 3.)

推荐答案

select * from (
  SELECT id,
  ROW_NUMBER() OVER(PARTITION BY merchant_Id, url ORDER BY id asc) AS Row
  FROM Photos
) dups
where 
dups.Row > 1

通过定制您要删除的记录到您的规格,随意播放订单。

Feel free to play with the order by to tailor the records you want to delete to your specification.

SQL Fiddle => http: //sqlfiddle.com/#!15/d6941/1/0

SQL Fiddle => http://sqlfiddle.com/#!15/d6941/1/0

编辑:不再支持SQL Fiddle for postgres 9.2;更新SQL Fiddle to postgres 9.3

edit: SQL Fiddle for postgres 9.2 is no longer supported; updating SQL Fiddle to postgres 9.3

这篇关于使用PostgreSQL查找重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆