select YY.* from ( select XX.ItemID from ( select ItemID from Parts where FromType = 6 and ContentID IN(select pp.contentid --,xSubSection.value('@Edit','varchar(1)') Editable from parts pp join (select Contentid, cast(config as xml) xConfig from contents) CC ON CC.ContentID= pp.ContentID outer apply xConfig.nodes('//SubSection') tSubSection(xSubSection) where fromtype in (2,6) and isnull(xSubSection.value('@Edit','varchar(1)'),'N')='N' group by pp.contentid having count(*) > 1) ) ah Cross Apply vefn_SiblingItems(ah.ItemID,null) XX Join Contents CC ON CC.ContentID=XX.ContentID) AH Cross Apply vefn_FindExternalTransitions(ItemID) YY