Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Getting non unique records with sequel

I'm using sequel in my project and I have problem with getting non unique record ids from db. I have the following table:

table = Sequel::Model.db[:csv_temp]

and records in that table looks like this:

table.all

[{:id=>1,
  :school_identifier=>"school_id_1",
  :school_name=>"school_name_1",
 },
 {:id=>2,
  :school_identifier=>"school_id_1",
  :school_name=>"school_name_2",
 }]

How can I get ids of records which school_identifier is not unique?

Upvotes: 0

Views: 66

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

Here is one way to do this, there are probably others:

DB[:csv_temp].
  where(:school_identifier=>DB[:csv_temp].
    select_group(:school_identifier).
    having{count{}.* > 2}).
  select_map(:id)

Upvotes: 1

Related Questions