Home Forums SQL Server 2005 SQL Server 2005 General Discussion How to join multiple tables (SQL) to find the difference between an order and shipped quantities? RE: How to join multiple tables (SQL) to find the difference between an order and shipped quantities?

  • kaminskidevelopment (7/12/2013)


    Let me get some better IDs for you - I just used some top IDs

    I put together an example of how you should post this stuff. It needs to be readily consumable so we can just hit f5 and start on the issue.

    I changed your Order table to MyOrder so I don't have to continuously wrap it with []. :w00t:

    CREATE TABLE [dbo].[MyORDER](

    [ID] [int] NOT NULL,

    [OrderRef1] [varchar](10) NULL,

    [OrderRef2] [varchar](10) NULL,

    [OrderDate] [varchar](10) NULL,

    [Shipped] [bit] NOT NULL,

    [ReceiptSent] [bit] NOT NULL,

    )

    CREATE TABLE [dbo].[orderITEMS](

    [ID] [int] NOT NULL,

    [OutboundDeliveryID] [int] NOT NULL,

    [OrderLineNr] [int] NULL,

    [ItemNumber] [varchar](18) NULL,

    [BatchNr] [varchar](10) NULL,

    [QtyToShip] [varchar](13) NULL,

    )

    CREATE TABLE [dbo].[Shipping](

    [ID] [int] NOT NULL,

    [Client] [int] NULL,

    [PO] [nvarchar](255) NULL,

    [Date] [datetime] NULL,

    )

    CREATE TABLE [dbo].[ShippingArchive](

    [ID] [int] NOT NULL,

    [OrigID] [int] NULL,

    [Client] [int] NULL,

    [ProductCode] [nvarchar](25) NULL,

    [LotID] [int] NULL,

    [Lot] [nvarchar](25) NULL,

    [Qty] [int] NULL,

    [StatusID] [int] NULL,

    [StatusDate] [datetime] NULL,

    )

    insert myORDER

    select 1525, 1181115159, null, '12/01/2013', 0, 0 union all

    select 1524, 1181115158, null, '10/01/2013', 0, 0

    insert orderITEMS

    select 1663, 1525, 901, 71565555, '2Y637N', 20 union all

    select 1662, 1524, 20, 122000600, NULL, 1

    insert shipping

    select 177620, 400, '0080014959', '2013-07-11 00:00:00.000' union all

    select 177618, 400, '0080014957', '2013-07-11 00:00:00.000'

    insert shippingArchive

    select 4262720, 0, 400, 71034600, 2838356, '13EM10643', 1, 177618, '2013-07-11 00:00:00.000' union all

    select 4262722, 0, 400, 71034600, 2838356, '13EM10643', 1, 177620, '2013-07-11 00:00:00.000'

    select * from myorder

    select * from orderITEMS

    select * from Shipping

    select * from ShippingArchive

    Notice how the sample data is inserts, that is what I mean by consumable.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/