I have a table whereby there is a Parent/Child relationship, where each of the children's ExternalCategoryCode
column data needs to be concatenated into the parents ExternalCategoryCode
.
Here is some example data that is corrupted, whereby the Parent record (Id=96)
ExternalCategoryCode
is NULL. The relationship is via the ParentId
field:
Id |Name |ExternalCategoryCode|ParentId
---|------------------------------|--------------------|---------
96 | Health & Personal Development| NULL | NULL
---|------------------------------|--------------------|---------
97 | Health | H1*;H2* | 96
---|------------------------------|--------------------|---------
98 | Personal Development | P1;P2;P3* | 96
---|------------------------------|--------------------|---------
99 | Other Health | OH* | 96
---|------------------------------|--------------------|---------
The data should look like this:
Id |Name |ExternalCategoryCode |ParentId
---|------------------------------|----------------------|---------
96 | Health & Personal Development| H1*;H2*;P1;P2;P3*;OH*| NULL
---|------------------------------|----------------------|---------
97 | Health | H1*;H2* | 96
---|------------------------------|----------------------|---------
98 | Personal Development | P1;P2;P3* | 96
---|------------------------------|----------------------|---------
99 | Other Health | OH* | 96
---|------------------------------|----------------------|---------
EDITs:
- The descendant level can be any levels deep, but the deep children
descendants need updating to the main Parent's
ExternalCategoryCode
. Take the last two records below (Id=100 and Id=101) both link to aParentId
97. Because Id=97ParentId
IS NOT NULL, we should continue up the tree until we find a record with aParentId
of NULL (Top most Parent). - Another question was regarding can the upper most parent's
ExternalCategoryCode
contain duplicates? Example below shows M2* twice. This is fine, because our business logic class libraries filter out duplicates if it finds any. If a record has a link in its
ParentId
(i.e. is a child) but has a NULLExternalCategoryCode
, these codes can be ignored.Id |Name |ExternalCategoryCode |ParentId ---|------------------------------|----------------------|--------- 96 | Health & Personal Development| H1*;H2*;P1;P2;P3*;OH*| NULL | | | | | M1;M2*; M2*;M3* | ---|------------------------------|----------------------|--------- 97 | Health | H1*;H2* | 96 ---|------------------------------|----------------------|--------- 98 | Personal Development | P1;P2;P3* | 96 ---|------------------------------|----------------------|--------- 99 | Other Health | OH* | 96 ---|------------------------------|----------------------|--------- 100| Medicine | M1;M2* | 97 ---|------------------------------|----------------------|--------- 101| Other Medicine | M2*;M3* | 97 ---|------------------------------|----------------------|--------- 102| Other Medicine 2 | NULL | 97 ---|------------------------------|----------------------|---------
There are other Parent/Child corruptions in the table as well. How do I:
Script this to down the table in question and concatentate the parent records
ExternalCategoryCode
with childExternalCategoryCode
data?List the Parent records which are updated. Usually when using
UPDATED
, this just shows something like this, whereby I would like to report this:(1 row(s) affected)
Techology Im using:
- SQL Server