Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Newbie T-SQL ? Using Joins


Newbie T-SQL ? Using Joins

Author
Message
jnesbit 63151
jnesbit 63151
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 34
Hello all, this my first time posting,but I have read several of the killer articles from the site. I am a newbie to T-SQL. I am trying to do the following. I have tried left outer join and using a subquery..results very from duplicates to an empty table.

Table1

Column1 Column2
A 1
B 1
C 1
A 2
B 2
C 2
D 2


Table 2

Column1
A
B
C
D

Output Table should look like as I am only interest in values that are in T2 that are not in T1 any give value of T1C2

Column1 Column2
D 1

Any light will be appreciated greatly
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24233 Visits: 37978
You are looking at something like this:



declare @Table1 table(
Col1 char(1),
Col2 int
);

declare @Table2 table(
Col1 char(1)
);

insert into @Table1
values ('A',1),
('B',1),
('C',1),
('A',2),
('B',2),
('C',2),
('D',2);

insert into @Table2
values ('A'),('B'),('C'),('D');

select * from @Table1;
select * from @Table2;

with UniqCol2 as (
select distinct Col2 from @Table1
), ExcludedData as (
select
t2.Col1,
uc.Col2
from
@Table2 t2
cross apply UniqCol2 uc
except
select
t1.Col1,
t1.Col2
from
@Table1 t1)
select Col1 from ExcludedData;




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)
jnesbit 63151
jnesbit 63151
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 34
Thanks that is excactly what I was looking for. I am not familiar with cross apply,but look forward to using it.
Cheers!
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