Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Compare two column with another tables column

Author  Topic 

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-07-06 : 01:05:12
Hi,

I have two culumns A,B in Table1 and Column C, D in Table2
I need to compare column combination of A and B with column C and D

Like if 1- 10 is available in table2 then yes else No.
Is this possible without looping.

Please help!

---------Table1-----------
A - B
1 - 10
2 - 11
3 - 12
4 - 13
5 - 14
6 - 15
----------- Table2-----
C - D
1 - 10
2 - 11
2 - 12
2 - 13
3 - 14
3 - 15
4 - 13
4 - 19
4 - 20
4 - 21
5 - 15
5 - 16
6 - 15

could someone help me here ASAP.

Thanks,
Pushp

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-07-06 : 03:46:19
You are not very clear.
Please post the needed result in relation to your sample ASAP.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-07-06 : 05:02:46
--------OUTPUT----------
A - B Result
1 - 10 YES in table2
2 - 11 YES in table2
3 - 12 YES in table2
4 - 13 YES in table2
5 - 14 NO
6 - 15 YES in table2

output maybe like above or may be anything alse that represent if table1 combination is available in table2
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-07-06 : 05:31:55
[code]---------Table1-----------
declare @table1 table(A varchar(10), B varchar(10))

insert @table1
select '1', '10' union all
select '2', '11' union all
select '3', '12' union all
select '4', '13' union all
select '5', '14' union all
select '6', '15'

----------- Table2-----
declare @table2 table(C varchar(10), D varchar(10))

insert @table2
select '1','10' union all
select '2','11' union all
select '2','12' union all
select '2','13' union all
select '3','14' union all
select '3','15' union all
select '4','13' union all
select '4','19' union all
select '4','20' union all
select '4','21' union all
select '5','15' union all
select '5','16' union all
select '6','15'

select
t1.A,
t1.B,
case when t2.C is null then 'NO' else 'YES' end as Result
from @table1 as t1
left join @table2 as t2 on t1.A = t2.C and t1.B = t2.D[/code]


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-07-06 : 07:18:49
thanks webfred that worked for me!!
thank you.......
Go to Top of Page
   

- Advertisement -