Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Exclude Records using join Expand / Collapse
Author
Message
Posted Tuesday, November 6, 2012 4:51 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 17, 2013 2:07 PM
Points: 171, Visits: 556
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
Post #1381749
Posted Tuesday, November 6, 2012 6:41 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:03 PM
Points: 3,609, Visits: 5,221
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!
Post #1381764
Posted Tuesday, November 6, 2012 8:15 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:55 AM
Points: 1,945, Visits: 2,860
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!

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

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.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1381775
Posted Tuesday, November 6, 2012 9:08 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:03 PM
Points: 3,609, Visits: 5,221
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!
Post #1381783
Posted Wednesday, November 7, 2012 2:41 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 10, 2014 10:51 PM
Points: 63, Visits: 286
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
Post #1381850
Posted Wednesday, November 7, 2012 4:44 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1381910
Posted Wednesday, November 7, 2012 7:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,115, Visits: 13,476
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
Post #1381962
Posted Wednesday, November 7, 2012 7:14 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:03 PM
Points: 3,609, Visits: 5,221
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!



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!
Post #1381969
Posted Wednesday, November 7, 2012 7:53 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 17, 2013 2:07 PM
Points: 171, Visits: 556
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 .
Post #1381992
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse