Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I am trying to do a search based on an inner join. I combine Birth, death and cemetery records. However, I am looking to update specific rows that may be in one of the tables.
The problem is that when I use an inner join it will only show those people that perfectly match between all three tables. So, if I have 100 people and only 80 of those have death and cemetery records then only those rows populate the data table. This means that 20 rows out of the 100 are not there. Any suggestions on how to proceed with finding the actual row number associated with the data from the inner join statement vs the tables it comes from

for instance,

Birth may be row 1 in the birth table
Death may be row 60 in the death table
and cemetery may be 70 in the cemetery table

I need to know these positions for my data update statement.

What I have tried:

I Have tried to find the row index, but it relates the current table being populated. Which gives the wrong row index in some cases.
Posted
Updated 20-Jan-19 18:53pm

Change it up to use an OUTER JOIN instead
SELECT    b.BirthIndex,    b.{other data}
     ,    d.DeathIndex,    d.{other data}
     ,    c.CemeteryIndex, c.{other data}
FROM      BirthRecords           b
LEFT OUTER JOIN DeathRecords     d ON b.PersonID = d.PersonID
LEFT OUTER JOIN CemeteryRecords  c ON b.PersonID = c.PersonID

This will bring up everyone that you have a record for in the Birth table, and will bring up any records that are present in the related tables for Death and/or Cemetery.

What you will need to account for in your code is that you are going to get NULLs for the DeathIndex and/or CemetaryIndex if there is no corresponding record.

One thing to consider would be to add another table for the people in general (PersonsTable) that has their main information, and use that as the "main" table and Left Join the others. It is entirely possible to end up with death and or cemetery records and not have a birth record locally.
   
Comments
Member 11856456 20-Jan-19 22:33pm
   
Interesting way to tackle the problem, only issue is this only assumes the row index of table 1 but not the other tables. Is there a way to call forth which row was used for each table.

For instance

birth table record 1 would be row 1, but the information for the other tables are different. Death index for this individual may be on row 5 and the cemetery may be on row 10. it is all dependent on what records were found first and then inserted into the database.
MadMyche 21-Jan-19 8:01am
   
That is why I put the table-index column from each, so that you could see the row number in each table.
You could also go with a FULL JOIN which would have all rows from all tables.
That is why I threw out the option of having a Person table with outer joins to births, deaths, and cemeteries
If you're trying to keep data in different tables "sync'd up" with each other solely by row number in the tables, you're doing it very, very wrong.

This is why tables should have primary keys and referencing keys in other tables, called foreign keys.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900