Hi @ahmed salah ,
Could you please provide any update or more details?
As suggested by Erland, you could refer below method using Between instead of UNION and check whether it is working.
select *
from #trades
where partid in (
select partid from #trades group by partid having count(*)=1)
and PartId not in
( select PartId from #trades a ,#mapping b
where a.CodeTypeId between iif(CodeTypeFrom>CodeTypeTo,CodeTypeFrom,CodeTypeTo) and iif(CodeTypeFrom>CodeTypeTo,CodeTypeTo,CodeTypeFrom))
and CodeTypeId=3030
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.