|
I have two tables. Table A and Table B. Table A has column of just yearweek Table B has values of yearweek, acct#, usage I want to combine the two table but notice in table B 200804 is missing. Because the acct did not report any usage. When I combine the two table I want 200804 to show a row which shows 0 for usage. I have tried a cross join but the dataset is so huge it will take too long to execute with a cartesian product outcome. Any idea how I can accomplish this without using a cross join? I tried a left join but they didnt work because I joined on yearweek so it just took the yearweeks that were listed.
(comments are locked)
|
|
Try an OUTER JOIN edit Hmm. I'm just running with the following code: That provides me with the following results: So you have NULLs in rows where the "real" data is missing. What you could do is change your final This returns the following: Outer Join didn't work. Still skipping over my week which I have no usage reported
Sep 21 '11 at 11:06 AM
sqlnewb
(comments are locked)
|
|
try this: select ta.YrWk, tb.YrWk, tb.Acct, tb.Usage from TableA ta full outer join TableB tb on ta.YrWk=tb.YrWk I tried this it didn't work. Still skipping over my week in table B and not adding in a 0 for usage
Sep 21 '11 at 10:54 AM
sqlnewb
@sqlnewb This does not appear to be possible, the full outer join should do the trick. Just make sure that you always display the YrWk value regardless whether it exists in both or just one of the tables. In other words, try this: select
isnull(ta.YrWk, tb.YrWk) YrWk,
tb.Acct, isnull(tb.Usage, 0) Usage
from TableA ta full outer join TableB tb
on ta.YrWk = tb.YrWk;What do you want to do with account column? As is, it will show up in the results as NULL (for not reported weeks) and if you need to display the misssing acount numbers instead of diplaying null then you need to include an Acct column from the Account parent table in the mix, something like this (assuming that you do have a parent account table which has Acct column with unique Acct value in every record). You can then afford go with left join :) select
ta.YrWk, ac.Acct, isnull(tb.Usage, 0) Usage
from TableA ta cross join Account ac
left join TableB tb
on ta.YrWk = tb.YrWk
and ac.Acct = tb.Acct
order by 2, 1;
Sep 21 '11 at 11:34 AM
Oleg
I really do not understand why the full outer join is not working. It should list all the values from table A but it is skipping over the weeks which are not listed in table B
Sep 21 '11 at 12:01 PM
sqlnewb
(comments are locked)
|
|
You can use a left outer join But if you need to apply a filter on table b, you need to add it in the join clause or use is null @Hakan when you write:
Sep 21 '11 at 11:16 AM
sqlnewb
It was just an example, I wanted to demonstrate how you need to write any filter for table2 to avoid reducing the final result from tableA.
Sep 21 '11 at 11:57 AM
Håkan Winther
(comments are locked)
|


can you post your query? you might have something going on in your query that's different that some of the demo queries on the answers.
Posting table create and insert scripts to create an entire reproduction scenario like in @ThomasRusthon 's answer would be helpful.