CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • merge duplicate relationships
Pages: [1]

Author Topic: merge duplicate relationships  (Read 3993 times)

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
merge duplicate relationships
May 18, 2010, 05:32:09 am
After I merged individuals and organisations I now have a lot of duplicate relationships: a lot of "employee of" relationships between the same person and the same organisation. Any idea how to get rid of those?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: merge duplicate relationships
May 18, 2010, 08:44:24 am

i suspect your best bet would be to write a sql query to get rid of these duplicate relationships

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
Re: merge duplicate relationships
May 21, 2010, 01:54:29 am
Thanks. I did. To share my solution to the community I'll describe it here.

I used 2 queries. One is to show the duplicates:

SELECT * FROM ( SELECT contact_id_a, `contact_id_b`, `relationship_type_id`,  `start_date` , `end_date` , `is_active`, count(id) count_id, max(id) max_id FROM `civicrm_relationship` GROUP BY contact_id_a, `contact_id_b`, `relationship_type_id`, `start_date`, `end_date`, `is_active` ) rel_double WHERE count_id > 1

It shows the duplicates and one of the relationship id's (the highest number) of the duplicate rows.

Then I use that id to delete 1 relationship per duplicate:
DELETE FROM civicrm_relationship WHERE id IN (SELECT max_id FROM ( SELECT contact_id_a, `contact_id_b`, `relationship_type_id`,  `start_date` , `end_date` , `is_active`, count(id) count_id, max(id) max_id FROM `civicrm_relationship`  GROUP BY contact_id_a, `contact_id_b`, `relationship_type_id`, `start_date`, `end_date`, `is_active` ) rel_double WHERE count_id > 1)

You can use the first statement to check if you have more then one relationship and use the second again until it says: 0 row(s) deleted.



xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: merge duplicate relationships
May 21, 2010, 02:49:19 am
Hey, thanks for sharing!

How fast is is ?

Did you have duplicate relationships on nonactive relations ? Might be much faster to add a where is_active=1.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
Re: merge duplicate relationships
May 21, 2010, 03:10:14 am
We now have a database with 9000 contacts and about 5500 relationships  (almost all "employee of") .
The first query above showed 351 duplicates (with 2 or more).

The second query was very fast and I had to do it 4 times to clear them all. Obviously that depends on the size of the database. But I think it cannot get much faster then direct SQL statements like this.

I do not have much inactive relationships at all yet. So in my case it does not make a difference.
But I think if every field in this table is the same you can remove the duplicates (unless you have custom fields to this relationship, then you have to add those as well to the query)
« Last Edit: May 21, 2010, 03:15:43 am by CatorgHans »

Laryn

  • I post frequently
  • ***
  • Posts: 192
  • Karma: 4
    • CEDC
  • CiviCRM version: 4.6.x
  • CMS version: Drupal 7
Re: merge duplicate relationships
August 24, 2012, 12:36:35 pm
Quote from: CatorgHans on May 21, 2010, 01:54:29 am
Thanks. I did. To share my solution to the community I'll describe it here.

I used 2 queries. One is to show the duplicates:

SELECT * FROM ( SELECT contact_id_a, `contact_id_b`, `relationship_type_id`,  `start_date` , `end_date` , `is_active`, count(id) count_id, max(id) max_id FROM `civicrm_relationship` GROUP BY contact_id_a, `contact_id_b`, `relationship_type_id`, `start_date`, `end_date`, `is_active` ) rel_double WHERE count_id > 1

It shows the duplicates and one of the relationship id's (the highest number) of the duplicate rows.

Then I use that id to delete 1 relationship per duplicate:
DELETE FROM civicrm_relationship WHERE id IN (SELECT max_id FROM ( SELECT contact_id_a, `contact_id_b`, `relationship_type_id`,  `start_date` , `end_date` , `is_active`, count(id) count_id, max(id) max_id FROM `civicrm_relationship`  GROUP BY contact_id_a, `contact_id_b`, `relationship_type_id`, `start_date`, `end_date`, `is_active` ) rel_double WHERE count_id > 1)

You can use the first statement to check if you have more then one relationship and use the second again until it says: 0 row(s) deleted.

Two years later, this still came in handy. Thanks!
CEDC...social justice by design

*Get support on the new CiviCRM help site. /laryn

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • merge duplicate relationships

This forum was archived on 2017-11-26.