Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

3 tables left outer join

 
   Database Forums (Home) -> Programming RSS
Next:  managing a list  
Author Message
iccsi

External


Since: Oct 07, 2011
Posts: 3



(Msg. 1) Posted: Fri Oct 07, 2011 8:15 am
Post subject: 3 tables left outer join
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have 3 tables and would like to have all data exists on table 1 even
does not exist in table 2 and tbale 3 are there any way to have left
outer join for 3 tables or I have to use 2 quries to complete?

Your information is great appreciated,

Here is table example data:

Table1

MyKey, My Data
1 MyData1
2 MyData2
3 MyData3
4 MyData4


Table2
MyKey, My Data
1 MyData5


Table3
MyKey, MyData
2 MyData6

I would like to have a result which include all MyKey.
I tried to use one query, but I can not get all records.

Your help is great appreciated,

iccsi

 >> Stay informed about: 3 tables left outer join 
Back to top
Login to vote
iccsi

External


Since: Oct 07, 2011
Posts: 3



(Msg. 2) Posted: Fri Oct 07, 2011 11:29 am
Post subject: Re: 3 tables left outer join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Oct 7, 1:58 pm, Henk van den Berg wrote:
> iccsi schreef op 07-10-2011 17:15:
>
>
>
>
>
> > I have 3 tables and would like to have all data exists on table 1 even
> > does not exist in table 2 and tbale 3 are there any way to have left
> > outer join for 3 tables or I have to use 2 quries to complete?
>
> > Your information is great appreciated,
>
> > Here is table example data:
>
> > Table1
>
> > MyKey, My Data
> >    1         MyData1
> >    2         MyData2
> >    3         MyData3
> >    4         MyData4
>
> > Table2
> > MyKey, My Data
> >    1         MyData5
>
> > Table3
> > MyKey, MyData
> >    2        MyData6
>
> > I would like to have a result which include all MyKey.
> > I tried to use one query, but I can not get all records.
>
> > Your help is great appreciated,
>
> > iccsi
>
> If you want the data from table1 that's not in table2 or table3, based
> on MyKey, then this might be what you're looking for. Simply do a UNION
> ALL for Table2 and Table3, then do a LEFT JOIN from Table1 and Table2/3:
>
> declare @T1 table(MyKey int, MyData char(15))
> declare @T2 table(MyKey int, MyData char(15))
> declare @T3 table(MyKey int, MyData char(15))
>
> insert into @t1
>         select 1, 'mydata1'
>         union select 2, 'mydata2'
>         union select 3, 'mydata3'
>         union select 4, 'mydata4'
> insert into @t2
>         select 1, 'mydata5'
> insert into @t3
>         select 2, 'mydata6'
>
> select
>         t1.MyKey
> from
>         @t1 a
> left join
>         (select * from @t2 union all select * from @t3) c
> on
>         a.MyKey = c.MyKey
> where
>         c.MyKey is null
>
> Best,
> Henk- Hide quoted text -
>
> - Show quoted text -

Thanks for helping,

iccsi

 >> Stay informed about: 3 tables left outer join 
Back to top
Login to vote
Henk van den Berg

External


Since: Oct 07, 2011
Posts: 3



(Msg. 3) Posted: Fri Oct 07, 2011 1:26 pm
Post subject: Re: 3 tables left outer join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

iccsi schreef op 07-10-2011 17:15:
> I have 3 tables and would like to have all data exists on table 1 even
> does not exist in table 2 and tbale 3 are there any way to have left
> outer join for 3 tables or I have to use 2 quries to complete?
>
> Your information is great appreciated,
>
> Here is table example data:
>
> Table1
>
> MyKey, My Data
> 1 MyData1
> 2 MyData2
> 3 MyData3
> 4 MyData4
>
>
> Table2
> MyKey, My Data
> 1 MyData5
>
>
> Table3
> MyKey, MyData
> 2 MyData6
>
> I would like to have a result which include all MyKey.
> I tried to use one query, but I can not get all records.

Not sure what you mean by this.
Can you post 'Table4' with columns MyKey/MyData with the desired results
please?
 >> Stay informed about: 3 tables left outer join 
Back to top
Login to vote
Henk van den Berg

External


Since: Oct 07, 2011
Posts: 3



(Msg. 4) Posted: Fri Oct 07, 2011 2:27 pm
Post subject: Re: 3 tables left outer join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

iccsi schreef op 07-10-2011 17:15:
> I have 3 tables and would like to have all data exists on table 1 even
> does not exist in table 2 and tbale 3 are there any way to have left
> outer join for 3 tables or I have to use 2 quries to complete?
>
> Your information is great appreciated,
>
> Here is table example data:
>
> Table1
>
> MyKey, My Data
> 1 MyData1
> 2 MyData2
> 3 MyData3
> 4 MyData4
>
>
> Table2
> MyKey, My Data
> 1 MyData5
>
>
> Table3
> MyKey, MyData
> 2 MyData6
>
> I would like to have a result which include all MyKey.
> I tried to use one query, but I can not get all records.
>
> Your help is great appreciated,
>
> iccsi
>

If you want the data from table1 that's not in table2 or table3, based
on MyKey, then this might be what you're looking for. Simply do a UNION
ALL for Table2 and Table3, then do a LEFT JOIN from Table1 and Table2/3:

declare @T1 table(MyKey int, MyData char(15))
declare @T2 table(MyKey int, MyData char(15))
declare @T3 table(MyKey int, MyData char(15))

insert into @t1
select 1, 'mydata1'
union select 2, 'mydata2'
union select 3, 'mydata3'
union select 4, 'mydata4'
insert into @t2
select 1, 'mydata5'
insert into @t3
select 2, 'mydata6'

select
t1.MyKey
from
@t1 a
left join
(select * from @t2 union all select * from @t3) c
on
a.MyKey = c.MyKey
where
c.MyKey is null

Best,
Henk
 >> Stay informed about: 3 tables left outer join 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Left Outer Join and Where Clause - Hi, I have two tables Table1 and Table2. I need to join Table2 on Table1_SID to get name for SID Column of Table1. But I need all rows of Table1. I can use a Left Outer Join to achieve this. Now I have a problem. There are rows in table2 and table1....

Outer join question - I have three tables: 1 - people 2 - peopleAddressLinkingTable 3 - Addresses I want to get all people and their street name. I thought that this would be simple and thought that the following query should work: select * from people, addresses.street..

why right outer join is needed? - hello, i am having one query regarding joins if in left join i reverse order of table it behaves like right join then why we need to seperate joins? thanks in advance.

Left join query problem - Hello everyone, I have a product list in a table with 10 items in it. It’s content is like: 1 Coco-cola 2 Pepsi cola 3 Sprite 4 Sunkiss 5 Mango Juice 6 Grape Juice 7 Root Beer 8 7-up 9 .. 10 .. I also have a sales list, which records the sales....

Problem with sorting in Full Outer Join table - Hello, I stucked on strange problem with data order in dataset. Any clue or suggests will be appreciated. Let's start from the beggining. We have to tables: Table1: Import Nr 18 19 20 21 22 23 24 25 26 27 28 29 32 33 34 Table2: Import Nr 20 21 23 25 26...
   Database Forums (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]