Outer Join Blues

  • Hi Guys,

    OK, pretty new to TSQL as you will discover. I am struggling with this one so any help appriciated.

    I have 2 tables as seen below and I am trying to join them. I know salesline has 3 records I am interested in and CUSTINVOICEJOUR as 3 related records but one of them has no salesid.

    As I understand it the query using the left outer join should bring back 3 records and just have null values in any column that is has no data in the salestable. Please advise, appricaite your time

    Jason

    SELECT *

    FROM CUSTINVOICEJOUR

    LEFT OUTER JOIN SALESLINE ON SALESLINE.SALESID = dbo.CUSTINVOICEJOUR.SALESID

    WHERE CUSTINVOICEJOUR.DATAAREAID = 'GAL'

    AND SALESLINE.DATAAREAID = 'GAL'

    AND CUSTINVOICEJOUR.invoiceaccount = 'sa13039'

  • Try this

    SELECT *

    FROM CUSTINVOICEJOUR

    LEFT OUTER JOIN SALESLINE ON SALESLINE.SALESID = dbo.CUSTINVOICEJOUR.SALESID

    and SALESLINE.DATAAREAID = 'GAL'

    WHERE CUSTINVOICEJOUR.DATAAREAID = 'GAL'

    AND CUSTINVOICEJOUR.invoiceaccount = 'sa13039'

    The where clause is processed after the join and filtering out the null values



    Clear Sky SQL
    My Blog[/url]

  • Dave has supplied the answer. Not to pimp my blog, but I recently blogged about this issue and the post may help you, http://wiseman-wiseguy.blogspot.com/2009/09/on-vs-where.html

  • "pimp my blog". Nice. :w00t:

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. ๐Ÿ˜‰

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Garadin (10/9/2009)


    "pimp my blog". Nice. :w00t:

    I'm not sure what else to call it when I reference my own blog. I feel kind of dirty doing it, but I think it's a good post and it applies.

  • Garadin (10/9/2009)

    --------------------------------------------------------------------------------

    "pimp my blog". Nice.

    Jack Corbett (10/9/2009)

    I'm not sure what else to call it when I reference my own blog. I feel kind of dirty doing it, but I think it's a good post and it applies

    And it is a good answer to the OP's question and an easy read ... be proud of it, no need to pimp it just say "read this item"

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Jack Corbett (10/9/2009)


    Garadin (10/9/2009)


    "pimp my blog". Nice. :w00t:

    I'm not sure what else to call it when I reference my own blog. I feel kind of dirty doing it, but I think it's a good post and it applies.

    Indeed, I don't know why you'd feel dirty at all. That's the entire purpose of a technical blog. The fact that you are writing things that will actually help people should make you feel better about the time you spend doing it. After all, it's only if nobody benefits from your blog or reads these articles you write that it's truly a waste of time. I just liked the phrase =).

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. ๐Ÿ˜‰

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Thanks for the support guys. I'm just uncomfortable with self-promotion. Of course, until I have 1000's of readers I guess I have to.

  • Jack Corbett (10/9/2009)


    ..until I have 1000's of readers I guess I have to.

    Count me in Jack ๐Ÿ™‚

    Piotr

    ...and your only reply is slร inte mhath

  • Sorry for bringing an old thread back to the top but this is a really useful thread. The information gleaned from reading is most helpful. Many thanks.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply