SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Exclude Records using join


Exclude Records using join

Author
Message
komal145
komal145
Say Hey Kid
Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)

Group: General Forum Members
Points: 681 Visits: 1043
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
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7217 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7217 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sony Francis @EY
Sony Francis @EY
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 293
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
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4964 Visits: 5478
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16077 Visits: 19534
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
Exploring Recursive CTEs by Example Dwain Camps
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7217 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
komal145
komal145
Say Hey Kid
Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)Say Hey Kid (681 reputation)

Group: General Forum Members
Points: 681 Visits: 1043
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 .
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search