select S1.id as edId,devname 
from vcom_edcoderinfo as S1,vcom_devreg
where vcom_devreg.devtypeid=%d and 
vcom_devreg.areaid=%d  and 
S1.id=vcom_devreg.id and 
EXISTS  
(select num 
 from  
(select top (select channlenum from  vcom_edcoderinfo as S2 where S2.id = S1.id) 
ROW_NUMBER()Over(order by shipid) as num from vcom_ini_devactionship 
) as tempTab
where num not in 
(select outputchannel 
 from vcom_devlink 
 where devid=S1.id and outputdevid <>%d)