November 6, 2012 at 4:51 pm
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
November 6, 2012 at 6:41 pm
komal145 (11/6/2012)
Hi friendsHow 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 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
November 6, 2012 at 9:08 pm
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 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
November 7, 2012 at 2:41 am
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
November 7, 2012 at 4:44 am
Sony Francis @EY (11/7/2012)
Try thisSELECT *
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')
November 7, 2012 at 7:00 am
Sony Francis @EY (11/7/2012)
Try thisSELECT *
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.
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
November 7, 2012 at 7:14 am
ChrisM@Work (11/7/2012)
Sony Francis @EY (11/7/2012)
Try thisSELECT *
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 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
November 7, 2012 at 7:53 am
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