Exclude Records using join

  • Hi friends

    How to Exclude Records using join . I usually do not in

    Example: I have a Table A and Table B

    I want to exclude all the loans from table A where Table B has condition with COLUMN_B = "Y"

    example In not in : select * from TableA where ColumnA not in ( Select * from TableB where ColumnA='Y')

    Thanks,

    Komal

  • komal145 (11/6/2012)


    Hi friends

    How to Exclude Records using join . I usually do not in

    Example: I have a Table A and Table B

    I want to exclude all the loans from table A where Table B has condition with COLUMN_B = "Y"

    example In not in : select * from TableA where ColumnA not in ( Select * from TableB where ColumnA='Y')

    Thanks,

    Komal

    Probably something like this:

    SELECT *

    FROM TableA a

    INNER JOIN TableB b ON a.LoanID = b.LoanID

    WHERE b.ColumnA <> 'Y'


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • CELKO (11/6/2012)


    Where is your DDL? Why did you think you did not have to follow minimal Netiquette? You do not know that rows are not records!

    I want to exclude all the loans from table A where Table B has condition with COLUMN_B = "Y"

    SELECT * FROM A

    EXCEPT

    SELECT * FROM B WHERE B.col_b = 'Y':

    I have the feeling that col_b such a stinking mess that it is a Boolean flag in SQL. Tell me that you are not that ignorant.

    Ouch! Dude that was pretty harsh!

    Regardless that is an alternative solution but only if both tables have exactly the same columns and as long as that never changes.

    Use of * on a SELECT statement is not recommended in any Prod environment.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Try this

    SELECT *

    FROM TableA a

    LEFT OUTER JOIN TableB b ON a.LoanID = b.LoanID

    WHERE b.ColumnA <> 'Y'

    When we use INNER JOIN it will consider only commom records. But the user wants to consider all records in first table

  • Sony Francis @EY (11/7/2012)


    Try this

    SELECT *

    FROM TableA a

    LEFT OUTER JOIN TableB b ON a.LoanID = b.LoanID

    WHERE b.ColumnA <> 'Y'

    When we use INNER JOIN it will consider only commom records. But the user wants to consider all records in first table

    If you don't need anything in output from TableB, then using NOT EXISTS will make more sense and most likely give better performance too.

    SELECT A.*

    FROM TableA AS A

    WERE NOT EXIST ( SELECT 1

    FROM TableB AS B

    WHERE B.[KeyColumnToJoinOn] = A.[KeyColumnToJoinOn]

    AND B.ColumnA='Y')

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Sony Francis @EY (11/7/2012)


    Try this

    SELECT *

    FROM TableA a

    LEFT OUTER JOIN TableB b ON a.LoanID = b.LoanID

    WHERE b.ColumnA <> 'Y'

    When we use INNER JOIN it will consider only commom records. But the user wants to consider all records in first table

    If you reference an outer-joined table column in the WHERE clause, you turn that join into an INNER join - unless the reference is to a null value [WHERE b.ColumnA IS NULL].

    If you want to filter an outer-joined table, put the filter into the join condition.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (11/7/2012)


    Sony Francis @EY (11/7/2012)


    Try this

    SELECT *

    FROM TableA a

    LEFT OUTER JOIN TableB b ON a.LoanID = b.LoanID

    WHERE b.ColumnA <> 'Y'

    When we use INNER JOIN it will consider only commom records. But the user wants to consider all records in first table

    If you reference an outer-joined table column in the WHERE clause, you turn that join into an INNER join - unless the reference is to a null value [WHERE b.ColumnA IS NULL].

    If you want to filter an outer-joined table, put the filter into the join condition.

    These are the reasons I always do better with DDL and sample data! :hehe:


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Hi here is the example:

    DROP Table #a

    DROp Table #b

    Create table #a

    (

    Col1 int

    ,COL2 varchar(10)

    )

    Create table #b

    (

    col1 int

    ,COL2 varchar(10)

    )

    insert into #a

    Values (1,'Y')

    insert into #a

    Values (2,'N')

    insert into #a

    Values (3,'Y')

    insert into #a

    Values (4,'N')

    insert into #b

    Values (1,'Y')

    insert into #b

    Values (2,'N')

    Select * from #a a where a.COL2 not in ( select Col2 from #b B where b.COL2='Y')

    Hope this helps!!! because I wanted to use join , not except .

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

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