Results 1 to 11 of 11

Thread: [RESOLVED] Show all possible items in one table -- outer join thing?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Resolved [RESOLVED] Show all possible items in one table -- outer join thing?

    This is a question pertaining to MSSQL 2008. Okay, I'm not sure how much information I have to give in order to get the point across. The basic idea is that I have a Stock table that holds 40 different items that can be added to an order for an outlet (store). I need to return all 40 stock items even if these stock items don't appear as on an order already, which is where I have a problem. I thought I could just outer join the Stock table or something, but I'm either not thinking clearly or just not seeing this. This is the query I've created to test, which returns only 27 records:

    Code:
    SELECT s.StockId
    FROM OutletOrder oo
    INNER JOIN OrderItem oi ON oo.OrderId=oi.OrderID
    LEFT OUTER JOIN Stock s on oi.StockID=s.StockID
    WHERE oo.OutletId=8 AND oo.RecieveDate IS NULL;
    and, for testing purposes, this is a dump of the relevant tables and some data: http://davidmil.es/sql/dump.sql (~115k)

    I would greatly appreciate any help!
    Like Archer? Check out some Sterling Archer quotes.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Show all possible items in one table -- outer join thing?

    You say that you want every stock item but you have the Stock table on the right-hand side of a LEFT OUTER JOIN. A LEFT OUTER JOIN includes every record from the left-hand table and a RIGHT OUTER JOIN includes every record from the right-hand table.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show all possible items in one table -- outer join thing?

    Thanks for the reply. Changing that join to a right outer join doesn't produce the desired result; I still only retrieve 27 records.

    Any other insight?
    Last edited by kows; Nov 25th, 2010 at 02:12 AM.
    Like Archer? Check out some Sterling Archer quotes.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Show all possible items in one table -- outer join thing?

    You need to also alter the Where clause so that it doesn't force data to come from the oi/oo tables, eg:
    Code:
    WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
    I personally would re-write it so that the table you always want data from is listed first in the From clause, eg:
    Code:
    SELECT s.StockId
    FROM Stock s 
    LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
    LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
    WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show all possible items in one table -- outer join thing?

    Thanks for the reply again!

    Quote Originally Posted by si_the_geek View Post
    I personally would re-write it so that the table you always want data from is listed first in the From clause, eg:
    Code:
    SELECT s.StockId
    FROM Stock s 
    LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
    LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
    WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
    I did this before because it made more sense to me, but I kept getting the same results. So I left it alone.

    Quote Originally Posted by si_the_geek View Post
    You need to also alter the Where clause so that it doesn't force data to come from the oi/oo tables, eg:
    Code:
    WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
    oo.OutletId isn't a nullable field, so OutletId will never be null, and neither is oi.OutletId/oi.StockId. Changing this doesn't do anything, but thank you for the suggestion.

    Would anyone be open to running the SQL file I've provided in order to play around with this? Thanks again in advance!
    Like Archer? Check out some Sterling Archer quotes.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show all possible items in one table -- outer join thing?

    Would anyone happen to have any insight on this? I've still not been able to produce a desired result after much playing around.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Show all possible items in one table -- outer join thing?

    Not sure what you are trying to do, are you just trying to get to StockID's? What are you trying to get from the other tables? Do you just want to return 40 items?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show all possible items in one table -- outer join thing?

    I need to return every StockID that exists in the Stock table, regardless of if that StockID exists in an OrderItem for an Order. So, 40 items need to be returned every time. The quantity and prices in the order and some other things will be calculated later on, but I simplified the query for posting on this forum.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Show all possible items in one table -- outer join thing?

    So why not just just select the StockId from the stock table? That will return all wither or not they have ever been ordered
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Show all possible items in one table -- outer join thing?

    Quote Originally Posted by GaryMazzone View Post
    So why not just just select the StockId from the stock table? That will return all wither or not they have ever been ordered
    My thoughts also.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show all possible items in one table -- outer join thing?

    But along with the stock items, the order information is needed to be displayed. What you're suggesting, after all, is what this query already does:
    Code:
    SELECT s.StockId
    FROM Stock s 
    LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
    LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
    WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
    I'm not sure if you're not understanding the problem, or what, but I solved my own problem by only joining the OutletOrder table on multiple conditions (essentially, move WHERE clause to JOIN clause):
    Code:
    LEFT OUTER JOIN OutletOrder oo
        ON oo.OrderId=oi.OrderID
            AND oo.OutletId=8
            AND oo.RecieveDate IS NULL
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width