GET HIERARCHICAL VALUES PRESENT IN SAME COLUMN OF A TABLE

Lets discuss about the parent and child relationship present in the same column of a table. That means both exists in the same column and there is always a identifier which says whether a record belongs to parent value or it belongs to child value. See the below data .

As it is clearly seen from the data, parent and child record can be easily identified from column c1. Here , we need to apply self join to get parent and child values in the same row. See the below Query.

select parent_id,child_id from
(select val as parent_id from dbo.DB_T3 where c1='P') A,
(select val as child_id from dbo.DB_T3 where c1='C') B
where parent_id=SUBSTRING(child_id,1,len(parent_id))




Related posts