There are 3 tables in my DB; Table 1 has 'Collateral', Table 2 has 'Loans', Table 3 is a multi-link table between 1 and 2; let's call it 'Loan_Collateral_link'.
A collateral can belong to 1 or more Loans, and a Loan can have multiple Collaterals.
What I want to achieve, is create a separate result set (or table) in which I group together all Loans and Collaterals which are in any way linked to eachother through different Loans and/or Collaterals.
Loans:
ID | name |
---|---|
Loan1 | ABC |
Loan2 | DEF |
Loan3 | GHI |
Loan4 | JKL |
Loan5 | MNO |
Collaterals:
ID | name |
---|---|
Coll1 | Col1 |
Coll2 | Col2 |
Coll3 | Col3 |
Loan_Collateral_link:
Loan_ID | Collateral_ID |
---|---|
Loan1 | Col1 |
Loan2 | Col1 |
Loan2 | Col3 |
Loan3 | Col2 |
Loan4 | Col2 |
Loan5 | Col1 |
Loan5 | Col3 |
So you see Loan1, Loan2 and Loan5 are sharing col1, so I want them to be grouped. Col3 should be in that same group, as it is linked through Loan2.
Loan4 and Loan3 are linked through Col2, so they should be a separate group.
The resultset would be something as per below:
Groups:
Group_ID | item_ID |
---|---|
Group1 | Loan1 |
Group1 | Loan2 |
Group1 | Loan5 |
Group1 | Col1 |
Group1 | Col3 |
Group2 | Loan3 |
Group2 | Loan4 |
Group2 | Col2 |
I'm trying to write a script, probably using a loop? to get this done. I was thinking to loop over each record in the Loan_Collateral_Link table, write every link that I find for the record into a temp_table. Then loop over this temp_table to find all linked records. However, I can't really seem to work it out conceptually, as the loop should somehow reference itself.
Something like;
--ForEach Loan_Collateral_Link Loan;
--If not exists - Insert LOAN_ID into TempTable
--If not exist - Insert COL_ID into TempTable
--ForEach TempTable COL_ID;
--Select * FROM Loan_Collateral_Link where Loan_ID or Col_ID matches
--> If not exists - Insert LOAN_ID / COL_ID into TempTable
But this seems a bit like an infite loop perhaps?
Perhaps I should order all Loans, than go over Loan per Loan, add all the Col's, and in the loop per loan select all other loans with this loan. Than go over all the new Cols and select all matching Loans. However, than I have new Loans, so I need to go back and fetch their possible Cols. So again, how many times do I loop? Use a flag per collateral and keep looping until all flags are fulfilled?