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.
| Author |
Topic |
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-04 : 13:36:53
|
| I need to have 3 table joins. Here's the question:1. I need all the ordering codes in both the tables. Will this query work. Their are some in table1 and some in table 2 and some in both. But table2 has most of it. 2 I need to based on b.ProductId get the Product name from a 3rd table table3 I am a newbie and this is complicated for me.Thanks,Here's what I have so far:______________________________________________________select b.ProductId, a.col1, b.col2from dbo.table1 aright join dbo.table2 bon a.ProductId = b.ProductIdorder by a. ProductIdSA |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-09-04 : 13:58:09
|
Its hard to tell without seeing some sample data and expected output. But I think you may need a FULL OUTER JOIN between table a and table b since you want all the rows from both.Something like this perhaps...select a.col1, b.col2,coalesce(c.ProductName,'')from dbo.table1 a full outer join dbo.table2 b on a.ProductId = b.ProductIdinner join dbo.table3 c on b.ProductId = c.ProductIdorder by b.ProductId |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-04 : 14:26:21
|
| The table1 has 783 recordstable2 has 11139and this query returns 16968 recordsAlso I noticed that there are some a. ProductId that do not exists in table2 and even with a full outer join do not show in the result set.Does this appear correct?SA |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-04 : 14:37:49
|
| What's not making sense to me is that its a full join. But there is productID ONLY in table1 which does not show up in the result set? How come? It is not there in table2SA |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-04 : 15:06:06
|
| select a.col1, b.col2,coalesce(c.ProductName,'')from dbo.table1 a full outer join dbo.table2 b on a.ProductId = b.ProductIdinner join dbo.table3 c on b.ProductId = c.ProductIdorder by b.ProductIdIgnore earlier posts!! My question Full outer join in this situation should show the productID which is in table1 (ie a) and not in table2 (ie b). But its not doing that here How Come? SA |
 |
|
|
|
|
|