I used to think I was good at SQL until I received this latest request at work. WeWe have a table that has a DefectID field, and a PreviousDefectID field, in order to link defect records to each other as they change through time. I
I need to link these fields together by creating a view that will have a third column, maybe called GroupID, so that each chain will receive its own GroupID. There
There are three different types of cases that need to be solved. A) Simple case of A->B->C. In the view, all three of these records would receive the same GroupID B) Two or more DefectIDs come from the same PreviousDefectID. In this case, the view would have a different GroupID for each new branch that is created. This can keep happening in more than one generation. C) A DefectID has two or more PreviousDefectIDs (comma seprated)
- Simple case of A->B->C. In the view, all three of these records would receive the same
GroupID
- Two or more
DefectID
s come from the samePreviousDefectID
. In this case, the view would have a differentGroupID
for each new branch that is created. This can keep happening in more than one generation. - A
DefectID
has two or morePreviousDefectID
s (comma seprated)
Note, there are more records in the resulting view than in the table, because when a single DefectIDDefectID
leads to multiple other DefectIDsDefectID
s, there is a new branch created for each chain and each gets their own GroupIDGroupID
number.