删除基于多个列的重复记录? [英] Remove duplicate records based on multiple columns?

查看:143
本文介绍了删除基于多个列的重复记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Heroku主办我的Ruby on Rails应用程序和一个或那样的原因,我可能有一些重复的行。

有没有办法基础上删除2个或更多的标准重复的记录,但保留了重复征收的只有1记录?

在我的使用情况下,我在我的数据库中的品牌和型号的关系的汽车。

 品牌型号
--- ---
姓名姓名
          年
          修剪
          MakeId
 

我想删除具有相同的名称,年份所有型号记录和修剪,但保持1的那些记录(的意义,我需要的记录,但只有一次)。我使用Heroku的控制台,这样我可以轻松地运行一些活动记录的查询。

有什么建议?

解决方案

 类模型

  高清self.dedupe
    #找到所有的模型和他们组按键,应该是共同的
    分组= all.group_by {|模型| [model.name,model.year,model.trim,model.make_id]}
    grouped.values​​.each做|重复|
      #我们要保持正确的第一个?
      first_one = duplicates.shift#或流行音乐的最后一个
      #如果有更多的离开了,他们是重复
      #所以全部删除
      duplicates.each {|双| double.destroy}#重复的,现在被破坏
    结束
  结束

结束

Model.dedupe
 

  • 找到所有
  • 在小组他们在你需要的唯一性
  • 循环散列的分组模式的值
  • 因为要保留删除的第一个值的一个副本
  • 删除其余

I'm using Heroku to host my Ruby on Rails application and for one reason or another, I may have some duplicate rows.

Is there a way to delete duplicate records based on 2 or more criteria but keep just 1 record of that duplicate collection?

In my use case, I have a Make and Model relationship for cars in my database.

Make      Model
---       ---
Name      Name
          Year
          Trim
          MakeId

I'd like to delete all Model records that have the same Name, Year and Trim but keep 1 of those records (meaning, I need the record but only once). I'm using Heroku console so I can run some active record queries easily.

Any suggestions?

解决方案

class Model

  def self.dedupe
    # find all models and group them on keys which should be common
    grouped = all.group_by{|model| [model.name,model.year,model.trim,model.make_id] }
    grouped.values.each do |duplicates|
      # the first one we want to keep right?
      first_one = duplicates.shift # or pop for last one
      # if there are any more left, they are duplicates
      # so delete all of them
      duplicates.each{|double| double.destroy} # duplicates can now be destroyed
    end
  end

end

Model.dedupe

  • Find All
  • Group them on keys which you need for uniqueness
  • Loop on the grouped model's values of the hash
  • remove the first value because you want to retain one copy
  • delete the rest

这篇关于删除基于多个列的重复记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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