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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 RE: Need help with the select statement!!

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-12-02 : 21:21:55
I have the following 2 tables. I use table1 to find values that are common in table2. Also, I have shown with a few columns but in the real thing there are about 10 columns I need to check

SQL Query:



select *
from tabel1 a
inner join table2 b on b.Name= a.Name
where a.Name is not null and
((a.code is not null and a.code = b.code)
or (a.Found is not null and a.Found = b.Found )
or (a. Description is not null and a. Description = b. Description))
UNION
select *
from tabel1 a
inner join table2 b on b.Found = a.Found
where a.Found is not null and
((a.code is not null and a.code = b.code)
or (a.[name] is not null and a.[name] = b.[name])
or (a. Description is not null and a. Description = b. Description))
UNION
select *
from tabel1 a
inner join table2 b on b.Code= a.Code
where a.Code is not null and
((a.[name] is not null and a.[name]= b.[name])
or (a.Found is not null and a.Found=b.Found )
or (a. Description is not null and a.Description = b.Description))
UNION
select *
from tabel1 a
inner join table2 b on b.Description = a. Description
where a.Description is not null and
((a.[name] is not null and a.[name]= b.[name])
or (a.Found is not null and a.Found = b.Found )
or (a.Code is not null and a.Code = b.Code))


Final Output Needed:



test4 1 BB Null - record based on table1 looks for (code=BB and Name=test4) gets record set as shown from table2

test4 1 BB Null - record based on table1 (name=test4 and Found=1) gets record set as shown from table2
test4 1 CC NULL

TEST8 0 TS Computer - record based on table1 (Found=0 and code=TS and description=Computer) gets
record set as shown from table2


Table1



Name Found code Description
test4 Null BB NULL
test4 1 Null Null
Null 0 TS Computer

Table2:



Name Found code Description
test6 0 BB Null
test6 0 CC Null
test4 1 BB Null
test4 1 CC NULL
TEST2 1 CC NULL
TEST1 NULL CC NULL
TEST8 0 TS Computer


Column Definition:
Found - bit
Code - char(2)

SA

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2009-12-03 : 06:16:42
This?


--structure
declare @Table1 table (Name varchar(10), Found bit, code char(2), Description varchar(50))
declare @Table2 table (Name varchar(10), Found bit, code char(2), Description varchar(50))
--/

--data
insert @Table1
select 'test4', Null, 'BB', NULL
union all select 'test4', 1, Null, Null
union all select Null, 0, 'TS', 'Computer'

insert @Table2
select 'test6', 0, 'BB', Null
union all select 'test6', 0, 'CC', Null
union all select 'test4', 1, 'BB', Null
union all select 'test4', 1, 'CC', NULL
union all select 'TEST2', 1, 'CC', NULL
union all select 'TEST1', NULL, 'CC', NULL
union all select 'TEST8', 0, 'TS', 'Computer'
--/

--calculation
select
isnull(a.Name, b.Name) as Name,
isnull(a.Found, b.Found) as Found,
isnull(a.code, b.code) as code,
isnull(a.Description, b.Description) as Description
from @Table1 a inner join @Table2 b
on (a.Name = b.Name or a.Name is null or b.Name is null)
and (a.Found = b.Found or a.Found is null or b.Found is null)
and (a.code = b.code or a.code is null or b.code is null)
--/


Ryan Randall - Yak of all trades
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -