一个exists查询的例子
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
where 1=1
and exists
( select * from (
select AM_SynthesisRPTTemplate.*,AM_SynthesisRPTItem.RPTItemName,AM_SynthesisRPTItem.DispNo
from AM_SynthesisRPTTemplate
join AM_SynthesisRPTItem on AM_SynthesisRPTItem.RPTItemID = AM_SynthesisRPTTemplate.RPTItemID
left join AM_SynthesisRPTElement on AM_SynthesisRPTElement.RPTElementID = AM_SynthesisRPTTemplate.RPTElementID
where CellType = 1
) as B
where B.RPTTemplateID = AM_SynthesisRPTData.RPTTemplateID
)
and BB.RPTID = 2 and CC.RPTItemID <> 14 and CC.RPTITemID <> 15
and code = 'DA619C04-1499-4E39-AD16-518835FF775C'
order by GroupID,dispNo --desc
--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
where 1=1
and exists
( select * from (
select AM_SynthesisRPTTemplate.*,AM_SynthesisRPTItem.RPTItemName,AM_SynthesisRPTItem.DispNo
from AM_SynthesisRPTTemplate
join AM_SynthesisRPTItem on AM_SynthesisRPTItem.RPTItemID = AM_SynthesisRPTTemplate.RPTItemID
left join AM_SynthesisRPTElement on AM_SynthesisRPTElement.RPTElementID = AM_SynthesisRPTTemplate.RPTElementID
where CellType = 1
) as B
where B.RPTTemplateID = AM_SynthesisRPTData.RPTTemplateID
)
and BB.RPTID = 2 and CC.RPTItemID <> 14 and CC.RPTITemID <> 15
and code = 'DA619C04-1499-4E39-AD16-518835FF775C'
order by GroupID,dispNo --desc