2010-09-08 11 views
0

J'essaie d'établir une table temporaire pour un rapport de demande d'inventaire en cours dans SQL 2008. Je peux obtenir les résultats que je recherche jusqu'à ce que je frappe une facture avec plusieurs lignes. Voici le code à ce jour:Compteur en cours d'exécution dans SQL 2008

--Gather Current Order Data 
DECLARE @b TABLE ([Planned Shipment Date] DATE, [Order] VARCHAR(15), [Line Number] VARCHAR(15), [Location] VARCHAR(15), [Item] VARCHAR(15), [QuantityUsed] INT) 

INSERT INTO @b 
SELECT [Planned Shipment Date], [Document No_], [Line No_], [Location Code], No_, CAST(SUM([Outstanding Quantity]) AS INT) 
FROM [STAGE].[dbo].[TRU$Sales Line] 
WHERE [Document No_] LIKE 'SO%' 
AND [Gen_ Prod_ Posting Group] IN ('TOY', 'AUDIO', 'BICYCLE') 
AND [Outstanding Quantity] <> 0 
GROUP BY [Document No_], [Location Code], [Line No_], [Planned Shipment Date], No_ 
ORDER BY [Planned Shipment Date], [Document No_], [Line No_], [Location Code], No_ 

--Gather Current Inventory Data 
DECLARE @a TABLE ([Item] VARCHAR(15), [Location] VARCHAR(15), [Inventory] INT) 

INSERT INTO @a 
SELECT [Item No_], [Location Code], CAST(SUM([Quantity]) AS INT) 
FROM [STAGE].[dbo].[TRU$Item Ledger Entry] 
GROUP BY [Item No_], [Location Code] 
ORDER BY [Item No_], [Location Code] 

DROP TABLE ##TEMP 
--Insert to a temp table for testing 
SELECT [Planned Shipment Date], [Location], [Item], [Order], [Line Number], 
[Outstanding Qty], [Total Inventory], [Running Order Total] 
INTO ##TEMP 
FROM 
(SELECT 
    [order].[Planned Shipment Date] 
    ,[order].[Location] 
    ,[order].[Item] 
    ,[order].[Order] 
    ,[order].[Line Number] 
    ,0 AS [Outstanding Qty] 
    ,(SELECT SUM(inventory) 
    FROM @a inv 
    WHERE inv.item = [order].[Item] 
    AND inv.location = [order].[Location]) AS [Total Inventory] 
    ,(SELECT SUM(QuantityUsed) 
    FROM @b prevorder 
    WHERE prevorder.item = [order].[Item] 
    AND prevorder.location = [order].[Location] 
    AND prevorder.[order] <= [order].[Order] 
    ) AS [Running Order Total] 
    FROM @b [order] 
) AS OrderExtended 
--WHERE [Total Inventory] < [Running Order Total] 
ORDER BY [Planned Shipment Date], [Order], [Line Number], [Location], [Item] ASC 
GO 
--Display outstanding quantity to ship on the temp table 
UPDATE ##TEMP 
SET [Outstanding Qty] = SL.[Outstanding Quantity] 
FROM ##TEMP T, [TRU$Sales Line] SL 
WHERE T.[Order] = SL.[Document No_] 
AND T.[Line Number] = SL.[Line No_] 
GO 

Quand je lance une requête telle que:

SELECT * FROM ##TEMP 
WHERE Item = '1011861' 
ORDER BY [Order], [Line Number] 

je reçois quelque chose comme (total de fonctionnement incorrect de qté en circulation):

Planned Shipment Date | Location | Item | Order | Line Number | Outstanding Qty | Total Inventory | Running Order Total 
2010-08-20    HQ  1011861 SO18727 6   3     49     103 
2010-09-10    HQ  1011861 SO18727 7   50    49     103 
2010-10-01    HQ  1011861 SO18727 8   34    49     103 
2010-08-20    HQ  1011861 SO18731 6   45    49     174 
2010-09-10    HQ  1011861 SO18731 7   26    49     174 
2010-08-20    HQ  1011861 SO19268 1   1     49     175 
2010-08-26    HQ  1011861 SO20476 8   1     49     176 
2010-08-26    HQ  1011861 SO20552 4   1     49     177 
2010-08-27    HQ  1011861 SO20630 8   2     49     179 

Qu'est-ce que Je m'attends à quelque chose comme (un vrai total cumulatif de la quantité en suspens):

Planned Shipment Date | Location | Item | Order | Line Number | Outstanding Qty | Total Inventory | Running Order Total 
2010-08-20    HQ  1011861 SO18727 6   3     49     3 
2010-09-10    HQ  1011861 SO18727 7   50    49     53 
2010-10-01    HQ  1011861 SO18727 8   34    49     87 
2010-08-20    HQ  1011861 SO18731 6   45    49     132 
2010-09-10    HQ  1011861 SO18731 7   26    49     158 
2010-08-20    HQ  1011861 SO19268 1   1     49     159 
2010-08-26    HQ  1011861 SO20476 8   1     49     160 
2010-08-26    HQ  1011861 SO20552 4   1     49     161 
2010-08-27    HQ  1011861 SO20630 8   2     49     163 

Il semble que lorsqu'une date d'expédition différente est incluse dans une commande (plus d'une ligne par commande), elle renvoie ma requête. Qu'est-ce que je rate? J'ai tellement regardé cela que j'ai peur de ne pas voir la solution évidente.

Merci pour toute aide, Doug

Répondre

0

Vous devez limiter [numéro de ligne] lorsque prevorder.Order = [commande] .order:

(SELECT SUM(QuantityUsed) FROM @b prevorder   
WHERE prevorder.item = [order].[Item] AND prevorder.location = [order].Location]   
AND (prevorder.[order] < [order].[Order] OR (prevorder.[order] = [order].[Order]   
AND prevorder.[Line Number] <= [order].[Line Number]))   
AS [Running Order Total] 

Il serait plus utile de commander le rapport par [date d'expédition prévue], ordre, [numéro de ligne].

(SELECT SUM(QuantityUsed) FROM @b prevorder   
WHERE prevorder.item = [order].[Item] AND prevorder.location = [order].Location] 
AND (prevorder.[Planned Shipment Date] < [order].[Planned Shipment Date] OR 
(prevorder.[Planned Shipment Date] = [order].[Planned Shipment Date]  
AND (prevorder.[order] < [order].[Order] OR (prevorder.[order] = [order].[Order]   
AND prevorder.[Line Number] <= [order].[Line Number])))   
AS [Running Order Total] 

Obtenir trop complexe? Utilisez une table avec une colonne Identity = RecordID dans la table #temp. Insérez les enregistrements dans l'ordre dans lequel ils doivent apparaître dans le rapport.

(SELECT SUM(QuantityUsed) FROM @b prevorder   
WHERE prevorder.Item = [order].[Item] AND prevorder.location = [order].Location] 
AND prevorder.RecordID <= [order].RecordID) AS [Running Order Total]