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
 Which join should I use?

Author  Topic 

sarorelasoul
Starting Member

29 Posts

Posted - 2009-07-15 : 14:42:04
Hi,

I have 3 tables Program(program_cd,acad_year,race_eth_cd ), Admissions(program_cd,term_cd, adm_ct,appl_ct) and Petition(program_cd,term_cd, pet_ct)

I need to join the 3 tables together so that I have one record
program_cd, acad_year, adm_ct, appl_ct, pet_ct
where adm_ct, appl_ct and pet_ct are the sum of term counts for each acad_year.
I teried joining the first 2 with a left join then joining the 3rd one with them with a left join too, but it doesn't work in case there are no matching records in the 2nd table but there are matcing records in the 3rd one.
Which type of joins should I use in this case?

Thanks

sarorelasoul
Starting Member

29 Posts

Posted - 2009-07-15 : 14:59:14
In other words I need to Program left join Admissions and Program left join Petition and ll joined in the same record
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-15 : 15:00:56
So you have joined third table with second table.
You should join third table with first table too.


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

sarorelasoul
Starting Member

29 Posts

Posted - 2009-07-15 : 15:03:37
I don't want to join the second table with the third table, because that would give me null data in case there is data in the 3rd one but no matching data in the second one.
I want to join the first and second and the first and 3rd and have one record as a result!!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-15 : 15:11:21
So please give example data, your query so far and expected output.


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

sarorelasoul
Starting Member

29 Posts

Posted - 2009-07-15 : 15:19:12
It worked I just needed to add another condition.

appretiate your help

Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-15 : 15:20:33
fine


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

- Advertisement -