I *think* this might be what you are looking for. select atcd.Descriptionfrom atc_desc atcd inner join (SELECT DISTINCT T1.ATCkod FROM ATC_tot T1 JOIN ATC_tot T2 ON T2.ATCkod = T1.ATCkod and T2.Typ_lakemedel LIKE '%'+@Kod2+'%' JOIN ATC_tot T3 on T3.ATCkod = T1.ATCkod AND T3.Typ_lakemedel LIKE '%'+@Kod3+'%' WHERE T1.Typ_lakemedel = @Kod) Kod on atcd.ATCkod = Kod.ATCkod
Are you sure you need to be doing those self joins? It almost seems like you could be doing:select atcd.Descriptionfrom atc_desc atcd inner join ATC_tot AT on atcd.ATCkod = AT.ATCkodwhere AT.Typ_lakemedel LIKE '%'+@Kod2+'%' OR AT.Typ_lakemedel LIKE '%'+@Kod3+'%' or AT.Typ_lakemedel = @Kod --<-- some logic I am missing here - brains says it ain't right...
*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here!