SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Outer Join


Outer Join

Author
Message
Lyn Davies-370583
Lyn Davies-370583
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 37
When attempting to select all records from one table and only those matching from a second, not all the records are being selected from the left outer join table. Has anyone any ideas please?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223640 Visits: 46297
Could you post the code please

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lyn Davies-370583
Lyn Davies-370583
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 37
Please see code below:

SELECT TOP 100 PERCENT dbo.genpremloc.level_2, dbo.genpremloc.level_5, dbo.genpremloc.gen_code, dbo.genpremloc.app_budg,
dbo.genpremloc.adj_budg, dbo.genpremloc.rev_budg, dbo.vwCrystalEstPremisesReport.LineValue, dbo.vwCrystalEstPremisesReport.OrdDate
FROM dbo.genpremloc LEFT OUTER JOIN
dbo.vwCrystalEstPremisesReport ON dbo.genpremloc.gen_code = dbo.vwCrystalEstPremisesReport.gen_code
WHERE (dbo.genpremloc.level_5 LIKE '5075%') AND (dbo.vwCrystalEstPremisesReport.OrdDate >= CONVERT(DATETIME, '2006-08-01 00:00:00', 102)) AND
(dbo.vwCrystalEstPremisesReport.OrdDate <= CONVERT(DATETIME, '2007-07-31 00:00:00', 102))
ORDER BY dbo.genpremloc.level_5, dbo.genpremloc.level_2
toniupstny
toniupstny
SSC Eights!
SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)

Group: General Forum Members
Points: 982 Visits: 940
Without knowing more about the interrelationship of the fields and tables, my initial guess would be your WHERE clause is limiting the result set?

Toni
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214396 Visits: 41979
Exactly...

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29389 Visits: 19002
This is one of those cases where your WHERE clause is turning your OUTER into an INNER....

You're only allowing for non-null "right" records (based on the WHERE clause, so it's not being treated as an outer join anymore....

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Lyn Davies-370583
Lyn Davies-370583
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 37
Thanks to all.

Smile
toniupstny
toniupstny
SSC Eights!
SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)

Group: General Forum Members
Points: 982 Visits: 940
There was an article in the newsletter about Derived Tables that offers a way to resolve your issue by taking the WHERE clause and creating a Derived Table for the subset of rows that meet the Where clause conditions then doing the JOIN on that subset. In that way you would get OUTER JOIN to return unmatched rows as you eliminated the filtering of the WHERE clause.

http://www.sqlservercentral.com/articles/DerivedTables/61388/

I am not totally sure but my understanding of the article would indicate the changed query would be similar to this (you will need to verify but the concept is there).

SELECT TOP 100 PERCENT dbo.genpremloc.level_2, dbo.genpremloc.level_5, dbo.genpremloc.gen_code, dbo.genpremloc.app_budg,
dbo.genpremloc.adj_budg, dbo.genpremloc.rev_budg, Derived.vwCrystalEstPremisesReport.LineValue, Derived.vwCrystalEstPremisesReport.OrdDate

FROM dbo.genpremloc LEFT OUTER JOIN

/* Derived table that covers the WHERE clause */
(SELECT dbo.genpremloc.level_5, dbo.vwCrystalEstPremisesReport.OrdDate,
dbo.vwCrystalEstPremisesReport.OrdDate
FROM dbo.genpremloc JOIN
dbo.vwCrystalEstPremisesReport ON dbo.genpremloc.gen_code = dbo.vwCrystalEstPremisesReport.gen_code
WHERE (dbo.genpremloc.level_5 LIKE '5075%') AND (dbo.vwCrystalEstPremisesReport.OrdDate >= CONVERT(DATETIME, '2006-08-01 00:00:00', 102)) AND
(dbo.vwCrystalEstPremisesReport.OrdDate <= CONVERT(DATETIME, '2007-07-31 00:00:00', 102))) as Derived




ON dbo.genpremloc.gen_code = Derived.vwCrystalEstPremisesReport.gen_code

ORDER BY dbo.genpremloc.level_5, dbo.genpremloc.level_2



Toni
Lyn Davies-370583
Lyn Davies-370583
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 37
Toni thanks for that I will give it a try.
humbleDBA
humbleDBA
SSC Eights!
SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)SSC Eights! (852 reputation)

Group: General Forum Members
Points: 852 Visits: 1508
Hi Lyn,

With regards to Toni's link and Derived Tables, please read the discussion area too as there are some errors in what was said in the article. Not saying its not the right way, as it might be, but be aware of the discussion points and test, test, test... Smile

HTH
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search