Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Get original order quantities and dates, and find any changes or additions to quantities in subsequent order version(s) RE: Get original order quantities and dates, and find any changes or additions to quantities in subsequent order version(s)

  • Down below is the SQL that creates the "Orders" table. It includes every version of an Order. In the code below, I used it to retrieve Order S20026. I returns its 7 versions.

    SELECT DISTINCT

    sha.[Bill-to Name]AS [CustomerNameBillTo]

    ,sha.[Bill-to Customer No_]AS [CustomerNumberBillTo]

    ,sha.[Sell-to Customer No_]AS [CustomerNumberSellTo]

    ,sha.[Sell-to Customer Name]AS [CustomerNameSellTo]

    ,sha.No_AS [SalesOrderNumber]

    ,sha.[Version No_]AS [VersionNumber]

    ,sla.[No_]AS [ItemNumber]

    ,sha.[Order Date]AS [OrderDate]

    ,sha.[Requested Delivery Date]AS [RequestedDeliveryDate]

    ,DATEDIFF(DAY, sha.[Order Date], sha.[Requested Delivery Date]) AS [LeadTimeReqDeliveryDate]

    ,sha.[Promised Delivery Date] AS [PromisedDeliveryDate]

    ,DATEDIFF(DAY, sha.[Order Date], sha.[Promised Delivery Date]) AS [LeadTimePromisedDeliveryDate]

    ,sla.QuantityAS [QuantityOrdered]

    ,sha.[Posting Date]AS [PostingDate]

    ,CONVERT(INT, REPLACE(sha.[Shipping Time], CHAR(2), '')) AS [ShippingTime]

    ,CASE

    WHEN YEAR(sha.[Posting Date]) <> '1753'

    THEN DATEADD(DAY, CONVERT(INT, REPLACE(sha.[Shipping Time], CHAR(2), '')), sha.[Posting Date])

    ELSE NULL

    ENDAS [ActualDeliveryDate]

    ,NULLAS [LeadTimeActualDeliveryDate]

    ,sla.[Quantity Shipped]AS [ShippedQuantity]

    FROM NAV.dbo.[Sales Header Archive] sha

    LEFT OUTER JOIN NAV.dbo.[Sales Line Archive] sla

    ON sha.[No_] = sla.[Document No_]

    AND sha.[Version No_] = sla.[Version No_]

    WHERE sla.[Type] = 2

    AND sha.No_ = 'S20026'

    I noticed that if I run your code against the Orders table the results are accurate.

    When I replace every instance of the Orders table with the sql above (which I think is twice in your code) then the results are different. It returns an additional version