rewrite join

  • Can we rewrite the outer query mention below particularly NOT IN statement of the outer query

    SELECT DISTINCT

    A.sno ,

    pm.ProductID ,

    A.ProductName ,

    2

    FROM

    (

    SELECT

    mer.sno ,

    SUBSTRING(mer.MATERIAL , 1 , CHARINDEX('.' , mer.MATERIAL) - 1)AS ProductName

    FROM

    MOB.EOR mer WITH (NOLOCK)

    LEFT OUTER JOIN TLA.Tanks tks WITH (NOLOCK) ON mer.sno = tks.TS

    WHERE tks.TS IS NULL

    AND MER.sno IS NOT NULL

    AND CHEMICAL = 1

    --AND MER.SoldTo = @SoldTo

    --AND MER.SHIPTO = @ShipTo

    )AS A

    LEFT OUTER JOIN TLA.ProductsMaster pm WITH (NOLOCK) ON pm.ProductName = A.ProductName

    WHERE a.ProductName NOT IN(

    SELECT

    pm.ProductName

    FROM

    TLA.ProductsMaster pm WITH (NOLOCK)

    INNER JOIN TLA.Tanks t WITH (NOLOCK) ON t.ProductId = pm.ProductID);

  • What's the problem? Performance? Try replacing the NOT IN with NOT EXISTS.

    edit: any special reason the query is full with "WITH (NOLOCK)"?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • the objective is to replace the NOT in with some joins

  • ramrajan (6/30/2014)


    the objective is to replace the NOT in with some joins

    OK, so why don't you?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • i am new to this so need help

  • Replace the NOT IN with a LEFT OUTER JOIN and filter on NULL values.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • ramrajan (6/30/2014)


    the objective is to replace the NOT in with some joins

    What are you expecting to get from this? It might affect performance and the result should be the same (unless you have NULL values on productname column on ProductMaster table).

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply