select FormatID,Name,Description
--,Data,PData,GData 
, isnull(x1StpSectLatData.value('@CompressSteps','varchar(255)'),isnull(x2StpSectLatData.value('@CompressSteps','varchar(255)'),x3StpSectLatData.value('@CompressSteps','varchar(255)'))) CompressSteps
, isnull(x1StpSectLatData.value('@PartialStepCompression','varchar(255)'),isnull(x2StpSectLatData.value('@PartialStepCompression','varchar(255)'),x3StpSectLatData.value('@PartialStepCompression','varchar(255)'))) PartialStepCompression
from (select FF.FormatID,FF.Name, FF.Description, Data,null PData,null GData
from Formats FF
where FormatID=1
Union ALL
select  FF.FormatID,FF.Name, FF.Description, FF.Data, PF.Data PData,null GData
from Formats FF
Join Formats PF ON FF.ParentID=PF.FormatID
Where PF.FormatID = 1 and FF.FormatID != 1
Union ALL
select  FF.FormatID,FF.Name, FF.Description, FF.Data, PF.Data PData, GF.Data GData
from Formats FF
Join Formats PF ON FF.ParentID=PF.FormatID
Join Formats GF ON PF.ParentID=GF.FormatID
Where GF.FormatID = 1 and FF.FormatID != 1
) T1
cross apply data.nodes('/PlantFormat/FormatData/SectData/StepSectionData/StpSectLayData') t1StpSectLatData(x1StpSectLatData)
outer apply pdata.nodes('/PlantFormat/FormatData/SectData/StepSectionData/StpSectLayData') t2StpSectLatData(x2StpSectLatData)
outer apply gdata.nodes('/PlantFormat/FormatData/SectData/StepSectionData/StpSectLayData') t3StpSectLatData(x3StpSectLatData)
Order by FormatID