select--AM_SynthesisRPTData.Code,AM_SynthesisRPTData.RPTTemplateID as DataTemplateID --AM_SynthesisRPTData.*,BB.*,CC.RPTItemName,CC.DispNo * from AM_SynthesisRPTData join AM_SynthesisRPTTemplate BB on BB.RPTTemplateID = AM_SynthesisRPTData.RPTTemplateID join AM_SynthesisRPTItem CC on CC.RPTItemID = BB.RPTItemID where1=1 andexists ( select*from ( select AM_SynthesisRPTTemplate.*,AM_SynthesisRPTItem.RPTItemName,AM_SynthesisRPTItem.DispNo from AM_SynthesisRPTTemplate join AM_SynthesisRPTItem on AM_SynthesisRPTItem.RPTItemID = AM_SynthesisRPTTemplate.RPTItemID leftjoin AM_SynthesisRPTElement on AM_SynthesisRPTElement.RPTElementID = AM_SynthesisRPTTemplate.RPTElementID where CellType =1 ) as B where B.RPTTemplateID = AM_SynthesisRPTData.RPTTemplateID ) and BB.RPTID =2and CC.RPTItemID <>14and CC.RPTITemID <>15 and code ='DA619C04-1499-4E39-AD16-518835FF775C' orderby GroupID,dispNo --desc