如何找到 SQL 组中 运行 总数的最小值?

How Do I Find the Minimum Value of a Running Total by Group In SQL?

我是所有事物 excel 和强力查询的长期用户,但在 SQL.

方面我是一个彻头彻尾的脑残菜鸟

我的项目的目标是按组找到 运行ning 总数中的最小值。

我已经弄清楚如何做基础 运行ning 总计。我的下一个想法是 运行 另一个针对我的 运行ning total 查询的查询,其中我 运行 运行ning total 列上的另一个聚合函数到 return 最小值。当我这样做时,查询永远停止,然后 return 出现错误...

我会注意到我正在 MS Access 中创建 SQL 代码,因为我目前无法访问我可以在工作中使用的 SQL 服务器。

所以首先我创建一个联合查询将多个表组合在一起并将该查询称为“组合”:

    SELECT [ItemCode], '1/1/2010' as [Date], 'IM' as [Type], [WarehouseCode], [QuantityOnHand] as [Qty]
    FROM [IM_ItemWarehouse]
    
    UNION ALL
    
    SELECT [ItemCode], [RequiredDate] as [Date], 'PO' as [Type], [WarehouseCode], [QuantityOrdered] - [QuantityReceived] as [Qty]
    FROM [PO_PurchaseOrderDetail]
    WHERE [QuantityOrdered] - [QuantityReceived] > 0

    UNION ALL 

    SELECT [ItemCode], [PromiseDate] as [Date], 'SO' as [Type], [WarehouseCode], ([QuantityOrdered] - 
    [QuantityShipped])*-1 as [Qty]
    FROM [SO_SalesOrderDetail]
    WHERE [QuantityOrdered] - [QuantityShipped] > 0;

然后我走老路,使用嵌套的 select 语句来计算我的 运行ning 总数,并将此查询称为“RunTotal”:

    SELECT t1.[ItemCode], t1.[WarehouseCode], t1.[Date], sum(t1.[Qty]) AS TotalByDate, (SELECT
    sum(t2.[Qty])
    FROM Combined t2
    WHERE t2.[ItemCode] = t1.[ItemCode] AND t2.[WarehouseCode] = t1.[WarehouseCode] AND t2.[Date] <= t1.[Date]) AS RunningTotal
    FROM Combined t1
    GROUP BY t1.[ItemCode], t1.[WarehouseCode], t1.[Date]
    ORDER BY t1.[ItemCode], t1.[WarehouseCode], t1.[Date];

然后...卡住...这是我尝试过的方法,但到目前为止,查询一直在旋转,或者 return 出现错误:

    SELECT [ItemCode], [WarehouseCode], min([RunningTotal])
    FROM RunTotal
    GROUP BY [ItemCode], [WarehouseCode];

预先感谢您提供的任何帮助...我知道我的 运行ning 总计算可以使用 window 函数更有效,但由于我正在写这些 SQL MS Access 中的语句我无法访问 OVER 和 PARTITION BY 等性感花哨的东西...

示例基础数据:

Style   Type    Whs Date        Qty
widget  On Hand NVR 1/1/2010    100
widget  On SO   NVR 7/15/2020   -30
widget  On PO   NVR 7/18/2020   50
widget  On SO   NVR 7/19/2020   -10
widget  On SO   NVR 7/20/2020   -60
gizmo   On Hand NVR 1/1/2010    100
gizmo   On SO   NVR 7/15/2020   -100
gizmo   On PO   NVR 7/18/2020   50
gizmo   On SO   NVR 7/19/2020   -20
gizmo   On SO   NVR 7/20/2020   -30

示例运行总计:

Style   Type    Whs Date        Qty  RunTotal
widget  On Hand NVR 1/1/2010    100  100 
widget  On SO   NVR 7/15/2020   -30  70 
widget  On PO   NVR 7/18/2020   50   120 
widget  On SO   NVR 7/19/2020   -10  110 
widget  On SO   NVR 7/20/2020   -60  50 
gizmo   On Hand NVR 1/1/2010    100  100 
gizmo   On SO   NVR 7/15/2020   -90  10 
gizmo   On PO   NVR 7/18/2020   50   60 
gizmo   On SO   NVR 7/19/2020   -20  40 
gizmo   On SO   NVR 7/20/2020   -10  30 

示例最终结果:

Style   Whs MinRunTotal
widget  NVR 50
gizmo   NVR 10

从语法上讲,您的第一个查询不能在 MS Access 中编译,因为 RunningTotal 相关聚合表达式应该包含在 GROUP BY 子句中。但实际上,GROUP BY 中可能不允许相关子查询,并且您也不希望在 聚合期间按此排名计算进行分组。此外,在 MS Access 运行 中,对基础 UNION 查询的复杂操作确实会产生性能问题。

考虑以下设置:

  1. 将基础联合查询转换为临时 table:

    SELECT * INTO mytemptable FROM myUnionQuery
    
  2. 计算运行总计(准聚合)。

    SELECT t.[ItemCode], t.[WarehouseCode], t.[Date], 
           (SELECT SUM(sub.[Qty]) FROM myTempTable sub
            WHERE sub.[ItemCode] = t.[ItemCode] 
              AND sub.[WarehouseCode] = t.[WarehouseCode]
              AND sub.[Date] <= t.[Date]) AS RunningQtyTotal
    FROM myTempTable t;
    
  3. [ItemCode][WarehouseCode] 级别聚合:

    SELECT q.[ItemCode], q.[WarehouseCode], MIN(q.[RunningQtyTotal]) AS MinRunQtyTotal
    FROM mySelectQuery q
    GROUP BY q.[ItemCode], q.[WarehouseCode]