Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Efficient Search Query needed - Please Help

Author  Topic 

Mickybee
Starting Member

29 Posts

Posted - 2005-05-24 : 06:39:17
Hi,

Im building a family tree database with a list of people in one table and the relationship in another.

Table 1 - People
ID Name
1 Andrew
2 Ben
3 Charlie
4 David
5 Edward

Table 2 - Relationships
ID PeopleStartID PeopleEndID Relationship Type
1 1 2 Son
2 2 3 Son
3 3 4 Nephew
4 4 5 Brother

There are a few other bits in the tables such as relationship type and dates but ive removed them to make it easier.

The problem is, Im trying to find a neat way of getting a query that will allow me (When doing a search on Andrew) to find that Andrew is related to Edward. Im a little reluctant to simply add in an extra row onto the Relationships table but have included it as the example of the thing I am looking for.

ID PeopleStartID PeopleEndID Relationship Type
5 1 5 Son/son/Nephew/Brother

The people table is currently quite large (10,000) records so it would be great if you clever bods could give me some pointers as to what I should be doing. Im currently performing multiple select statements to build up the view which is very slow but it does give me the result Im looking for.

Your help would be hugely appreciated.

Many thanks in advance
Mike



nr
SQLTeam MVY

12543 Posts

Posted - 2005-05-24 : 11:31:13
What if there are several relationship paths between Andrew and David?
What happens if someone marries their cousin? (legal in the uk).

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Mickybee
Starting Member

29 Posts

Posted - 2005-05-24 : 11:51:39
There are a number of relationship paths between people, I tried to remove some of the complex ones to keep the question as simple as possible.

It gets a little confusing but sometimes people at the top of the tree are related to people at the bottom in a number of ways.

The example above showed a simple route from Andrew to Edwars but there may also be another route with different people from Andrew to Edward e.g. through another marriage or marriage in the same family.

Where this occurs I will also add records like (apologies for the crap example, its more realistic with larger datasets:

Table 2 - Relationships
ID PeopleStartID PeopleEndID Relationship Type
1 1 2 Son
2 2 3 Son
3 3 4 Nephew
4 4 5 Brother

5 1 6 Step Cousin
6 6 5 Nephew

In the silly example above, I will do a searh on person1 and the end result will be that they are related to person5 and Person6

Person5 as Son's, Son's Nephew's Brother
Person6 as StepCousin's Nephew

p.s. it all sounds rather scary now but I assure you, when you put in real people it does work.

Im trying to get back a list of all the end-relationships (person5/6) when starting at a single person1 with all the relationship names in between. It would be easier to get them back as a table but Im not too bothered.

Thanks for your help in advance

Mike

Go to Top of Page

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2005-05-25 : 11:13:44
I see no problem with having a Person table and a Person_Relationships table that holds the PersonID, and the RelatedToPersonID and the RelationshipType.

It sounds like you are setting this up just like the real world would have it. Put indexes on both "PersonID" columns, and on the relationship type. That will speed up searching. And don't worry about the size of the table. My Person_Relationships table has over 38,000 records in it and our stored procedures that reference it are still lightning fast.

Aj
Go to Top of Page
   

- Advertisement -