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)
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] "Uri Dimant" <urid@iscar.co.il> wrote in message news:OtjPKsjLHHA.3560@TK2MSFTNGP02.phx.gbl... > 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 > > > > > <mikejacobz@gmail.com> wrote in message > news:1167720118.492609.241640@v33g2000cwv.googlegroups.com... >> 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) >> > >
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] <mikejacobz@gmail.com> wrote in message news:1167720118.492609.241640@v33g2000cwv.googlegroups.com... > 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) >
Thanks everyone for your help Regards Mike [quoted text, click to view] Sylvain Lafontaine (fill the blanks, no spam please) wrote: > 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) > > > "Uri Dimant" <urid@iscar.co.il> wrote in message > news:OtjPKsjLHHA.3560@TK2MSFTNGP02.phx.gbl... > > 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 > > > > > > > > > > <mikejacobz@gmail.com> wrote in message > > news:1167720118.492609.241640@v33g2000cwv.googlegroups.com... > >> 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) > >> > > > >
Don't see what you're looking for? Try a search.
|