SQL 2008 中的运行计数器

发布于 2024-09-17 20:46:57 字数 4682 浏览 3 评论 0原文

我正在尝试在 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

当我运行一个查询时,例如:

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

我得到类似的东西(未完成数量的错误运行总计):

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

我期望的是类似的东西(未完成数量的真实运行总计):

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

似乎当订单中包含不同的发货日期(每个订单超过一行),这使我的查询失败。我缺少什么?我已经看过太多了,恐怕我看不到明显的解决方案。

感谢您的帮助, 道格

I'm trying to establish a temp table for a running inventory demand report in SQL 2008. I can get the results that I am looking for until I hit an invoice with multiple lines. Here is the code so far:

--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

When I run a query such as:

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

I get something like (incorrect running total of outstanding qty):

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

What I am expecting is something like (a true running total of outstanding qty):

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

It seems that when a different shipment date is included on an order (more than one line per order) it throws my query off. What am I missing? I have looked at this so much that I'm afraid that I can't see the obvious solution.

Thanks for any help,
Doug

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2024-09-24 20:46:57

当 prevorder.Order=[订单] 时,您必须限制 [行号]。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]

按 [计划发货日期]、订单、[行号] 来订购报告会更有用。

(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]

变得太复杂?使用具有 Identity Column = RecordID 的表来代替 #temp 表。按照记录在报告中出现的顺序插入记录。

(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]

You have to limit [Line Number] when prevorder.Order=[order].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]

It would be more useful to order the report by [Planned Shipment Date], Order, [Line Number].

(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]

Getting too Complex? Use a table with an Identity Column = RecordID intead of the #temp table. Insert the records in the order they should appear in the report.

(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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文