There is a loop in this query, in the last where condition. and this causes a severe problem to the performance of SQL.
I have no idea about how to modify it.
select pr.tavpun
from mta110 pr
where pr.taisoc = mta110.taisoc
and pr.taitar = mta110.taitar
and pr.taydat = mta110.taydat
and pr.tairef = mta110.tairef
and pr.tatind = (select max(pr2.tatind) from mta110 pr2
where pr2.taisoc = mta110.taisoc
and pr2.taitar = mta110.taitar
and pr2.taydat = mta110.taydat
and pr2.tairef = mta110.tairef
and pr2.tatind <= mgc100.gntind)) AS SalesPrice
CREATE TABLEandCREATE INDEXstatements of the table. Show the execution plan. And add some sample data asINSERT INTOstatements and the desired result with that sample data along with an explanation about the logic behind your query.mta110has no context as an alias; so the clausepr.taisoc = mta110.taisocwill error with an error about the unknown object or aliasmta110. Also, when wouldtaisocnot have a value oftaisocapart from when it'sNULL? If you are checking forNULLthen you should just be usingtaisoc IS NOT NULL.mta110is not defined in the outer query.