Link to home
Start Free TrialLog in
Avatar of yddadsjd95
yddadsjd95

asked on

Complicated Query

Good Day to all, I have a query that appears complicated to me.

I have an application for a church and I have a Family Tree feature that keeps track of who is related to who in the Church. So if John and Mary Doe are married, and John passes away, a few days before the anniversary of his death, I want the computer to automatically print Mary's name and Identify that her husband has been deceased for one year so the Church can send her an occasion card. In addition, if someone has a family member who passes away and is not a member of the church, that information will be kept in a separate table. Let's say in the above example, Mary's Sister is not a member of the church, and she passes away, then her departure information will be kept in a separate table. Consequently, I am attempting to create a query that pulls deceased information from two tables. Here is the list of related tables and what I need them to do based on the above example:

1) tblMember - Mary Doe is a member and has relatives who are members and non-members of the church who have passed away.
2) tblFamilyTree - Shows that John is related to Mary as her husband
3) tblImportantDates2 - Shows that John passed away on 1/1/2006, and that the event was "Date of Death"
4) tblFamilyTreeNonMember - Shows that Mary's sister who is not a member of the church passed away.
5) tblRelationship - Shows that John, a member of the church was related to Mary as her husband.

Two problems here: First I only want to pull information based on the the member, not who the member is related too, which is what my query is doing; second, and when I add tblFamilyTreeNonMembers, the one test record that is in there shows up for every record in the tblFamilytree for the person they are related too. I only want him/them to show up on one line: Here is the SQL without tblFamilyTrr4NonMember:

SELECT tblMembers.MemberID, tblMembers.Title, tblMembers.FirstName, tblMembers.LastName, tblFamilyTree.RelativeMemberID, tblFamilyTree.RelationshipID, tblImportantDates2.EventID, tblImportantDates2.EventDate
FROM tblRelationship INNER JOIN (tblMembers INNER JOIN (tblImportantDates2 INNER JOIN (tblFamilyTree INNER JOIN tblMembers AS tblMembers_1 ON tblFamilyTree.RelativeMemberID = tblMembers_1.MemberID) ON tblImportantDates2.MemberID = tblFamilyTree.RelativeMemberID) ON tblMembers.MemberID = tblFamilyTree.MemberID) ON tblRelationship.RelationshipID = tblFamilyTree.RelationshipID
WHERE (((tblImportantDates2.EventID)=7));


and here is the SQL when tblFamilyTreeNonMember is added:

SELECT tblMembers.MemberID, tblMembers.Title, tblMembers.FirstName, tblMembers.LastName, tblFamilyTree.RelativeMemberID, tblFamilyTree.RelationshipID, tblImportantDates2.EventID, tblImportantDates2.EventDate, tblFamilyTreeNonMember.[Full Name], tblFamilyTreeNonMember.RelationshipID, tblFamilyTreeNonMember.DeceasedDate
FROM (tblRelationship INNER JOIN (tblMembers INNER JOIN (tblImportantDates2 INNER JOIN (tblFamilyTree INNER JOIN tblMembers AS tblMembers_1 ON tblFamilyTree.RelativeMemberID = tblMembers_1.MemberID) ON tblImportantDates2.MemberID = tblFamilyTree.RelativeMemberID) ON tblMembers.MemberID = tblFamilyTree.MemberID) ON tblRelationship.RelationshipID = tblFamilyTree.RelationshipID) INNER JOIN tblFamilyTreeNonMember ON tblMembers.MemberID = tblFamilyTreeNonMember.MemberID;

this is probably not as complicated as I have attempted to describe.

Any suggestions?

Thanks in advance?

David
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

From a database normalization standpoint, your structure is making this complex.  What you are recording is 'people'.  Every 'people' has attributes, and being a member of the church or not could be considered an attribute.  In this case, why keep a separate table when a yes/no field will suffice to tell the difference?

My advice is to combine tblFamilyTree and tblFamilyTreeNonMember into a single table with a yes/no field making the distinction.  If you are not using additional fields for tblMembers, I would advise doing the same with that...merge it into tblFamilyTree (or the new tblPeople?) using a yes/no to indicate membership.  Now instead of the complicated joins, you should be able to query based on some simple where clauses.
Avatar of yddadsjd95
yddadsjd95

ASKER

Thank you for the advice. I combined tblFamilyTree with tblFamilyTreeNonMember, but I still had a problem: Because of referential integrity, tblFamily tree requires that a field - RelativeMemberID - have a value, and since the nonMember relatives are not part of the membership rolls, I was unable to save that record. However, I did a before update routine and made the RelativeMemberID.value = MemberID.Value. I also had to take one other step: When the subform was displayed that had the deceased non-member relatives, it opened with all of the member relatives as well. So I had to place a criteria in the RelativeMemberID field that only those records where the MemberId.Value = RelativeMemberID.Value.

It's working so far, but this is only step one of a two step process. I'll keep you posted how it works in the second step.

David
Hi Routinet, I am now attempting to create the query that gathers all relatives of a particular member who have passed away - members and non-members. All of the tables that I described above still exist, with the exception of tblFamilyTreeNonMember. Since I combined that table with tblFamilyTree, I had to add two fields to tblFamilyTree - FullName and DateDeceased. the form allows me to input the data on deaths of non-member relatives as I explained in my last response. However, when I execute the query that I have created, I receive a query record for EventID (1-8) for each deceased non-member, so I am getting 8 records for each deceased non-member. When I remove tblImportantDates2, that error goes away, but I need that table, because if any church members have passed away, the date of their death will be in tblImportantDates2. (This table keeps track of 16 events, or as many as the user wants, that occur in people's lives.) I want the query to check this table to see if a church relative has passed away, and if so, I grab the DeceasedDate. Here is the SQL:

SELECT tblMembers.MemberID, tblMembers.Title, tblMembers.FirstName, tblMembers.LastName, tblFamilyTree.RelativeMemberID, tblFamilyTree.RelationshipID, tblImportantDates2.EventID, tblImportantDates2.EventDate, tblFamilyTree.FullName, tblFamilyTree.DateDeceased
FROM tblRelationship INNER JOIN (tblMembers INNER JOIN (tblImportantDates2 INNER JOIN (tblFamilyTree INNER JOIN tblMembers AS tblMembers_1 ON tblFamilyTree.RelativeMemberID = tblMembers_1.MemberID) ON tblImportantDates2.MemberID = tblFamilyTree.RelativeMemberID) ON tblMembers.MemberID = tblFamilyTree.MemberID) ON tblRelationship.RelationshipID = tblFamilyTree.RelationshipID
WHERE (((tblFamilyTree.DateDeceased) Is Not Null)) OR (((tblImportantDates2.EventID)=7));

Any suggestions?

Thanks,

David
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial