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


NOT IN based on two fields


NOT IN based on two fields

Author
Message
Chrissy321
Chrissy321
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1244 Visits: 4714
Please help me to not use this silly concatenation method. I want to select all from the first table when the combination of two fields is not in the second.

What;s the accepted way to do this?

DECLARE @Test1 TABLE (Field1 varchar(1), Field2 int)
INSERT INTO @Test1
SELECT 'A',1 UNION
SELECT 'B',2 UNION
SELECT 'C',3

DECLARE @Test2 TABLE (Field1 varchar(1), Field2 int)
INSERT INTO @Test2
SELECT 'B',2 UNION
SELECT 'C',3


SELECT * FROM @Test1
WHERE
Field1 + CONVERT(varchar(1),Field2) NOT IN
(
SELECT Field1 + CONVERT(varchar(1),Field2) FROM @Test2
)
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15131 Visits: 14396
I may be making it too simple and missing some nuance of what you are after, but I think you just want a LEFT JOIN.

SELECT  t1.Field1,
t1.Field2
FROM @Test1 t1
LEFT JOIN @Test2 t2 ON t1.Field1 = t2.Field1
AND t1.Field2 = t2.Field2
WHERE t2.Field1 IS NULL;



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40424 Visits: 38567
This works also:



DECLARE @Test1 TABLE (Field1 varchar(1), Field2 int);
INSERT INTO @Test1
SELECT 'A',1 UNION
SELECT 'B',2 UNION
SELECT 'C',3 ;

DECLARE @Test2 TABLE (Field1 varchar(1), Field2 int);
INSERT INTO @Test2
SELECT 'B',2 UNION
SELECT 'C',3 ;

select * from @Test1 t1
where not exists(select 1 from @Test2 t2 where t1.Field1 = t2.Field1 and t1.Field2 = t2.Field2);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89379 Visits: 45284
Chrissy321 (3/21/2013)

SELECT * FROM @Test1
WHERE
Field1 + CONVERT(varchar(1),Field2) NOT IN
(
SELECT Field1 + CONVERT(varchar(1),Field2) FROM @Test2
)


SELECT * FROM @Test1 t2 
WHERE NOT EXISTS (SELECT 1 FROM @Test2 t2 WHERE t1.Field1 = t2.Field1 AND t1.Field2 = t2.Field2)



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Chrissy321
Chrissy321
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1244 Visits: 4714
Thank you all once again.
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