all groups > sql server programming > january 2007 >
You're in the

sql server programming

group:

Union question


Union question mikejacobz NO[at]SPAM gmail.com
1/1/2007 10:41:58 PM
sql server programming:
Hi, I have two tables which form a master table via a union.

e.g.

SELECT A,B,C,D
FROM
(SELECT A,B,C,D FROM TableA UNION SELECT A,B,C,D FROM TableB)

What I want to be able to do is see if attribute A in TableB exists in
Table A.
If so I want to be able to add this to the "Master Select" statement as
another attribute (We'll call it AExists).

Kind of like

SELECT A,B,C,D, AExists
FROM
(SELECT A,B,C,D,NULL as AEXISTS FROM TableA UNION SELECT A,B,C,D FROM
TableB WHERE A Exists in TableA As AExists)
Re: Union question Sylvain Lafontaine
1/2/2007 3:21:54 AM
From his description, another possibility could be:

select a,b,c, Null as AExists from #a
union all
select a,b,c, Case When Exists (select * from #a where #b.a=#a.a) Then 1
Else 0 End as AExists from #b

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


[quoted text, click to view]

Re: Union question Uri Dimant
1/2/2007 8:55:44 AM
Hi

create table #a (a int,b int,c int)
insert into #a values (10,20,30)
insert into #a values (20,1000,30000)

create table #b (a int,b int,c int)
insert into #b values (10,500,300)



select a,b,c from #a
union all
select a,b,c from #b where exists (select * from #a where #b.a=#a.a)

if it does not help, please post an expected result




[quoted text, click to view]

Re: Union question mikejacobz NO[at]SPAM gmail.com
1/2/2007 2:06:45 PM

Thanks everyone for your help
Regards
Mike

[quoted text, click to view]
AddThis Social Bookmark Button