SQL Server-需要在多个返回的记录中求和

时间:2018-07-10 21:15:02

标签: sql sql-server groovy jasper-reports

在以下查询中,我试图在项目SUM的两个位置上使TotalQty达到6112040,但是到目前为止,我仍然无法做到这一点。我确实需要将6112040的两行分开以捕获不同的位置。

此查询使用名为Java.Groovy的内容输入Jasper ireport。尽管如此,所打印的PDF都没有没有成为时尚的或没有染成棕色的。也许有人也可以解决该问题,但是此SUM问题优先考虑

我知道戈登·利诺夫(Gordon Linoff)将在大约一个小时内上线,因此也许他可以提供帮助。

DECLARE @receipt INT 
SET @receipt = 20 

SELECT
    ent.WarehouseSku AS WarehouseSku,
    ent.PalletId AS [ReceivedPallet],
    ISNULL(inv.LocationName,'') AS [ActualLoc],
    SUM(ISNULL(inv.Qty,0))  AS [LocationQty],
    SUM(ISNULL(inv.Qty,0)) AS [TotalQty],
    MAX(CAST(ent.ReceiptLineNumber AS INT)) AS [LineNumber],
    MAX(ent.WarehouseLotReference) AS [WarehouseLot],
    LEFT(SUM(ent.WeightExpected),7) AS [GrossWeight],
    LEFT(SUM(inv.[Weight]),7) AS [NetWeight]
FROM WarehouseReceiptDetail AS det
INNER JOIN WarehouseReceiptDetailEntry AS ent
    ON det.ReceiptNumber = ent.ReceiptNumber
    AND det.FacilityName = ent.FacilityName
    AND det.WarehouseName = ent.WarehouseName
    AND det.ReceiptLineNumber = ent.ReceiptLineNumber
LEFT OUTER JOIN Inventory AS inv
    ON inv.WarehouseName = det.WarehouseName
    AND inv.FacilityName = det.FacilityName
    AND inv.WarehouseSku = det.WarehouseSku
    AND inv.CustomerLotReference = ent.CustomerLotReference
    AND inv.LotReferenceOne = det.ReceiptNumber
    AND ISNULL(ent.CaseId,'') = ISNULL(inv.CaseId,'')
WHERE
        det.WarehouseName = $Warehouse
    AND det.FacilityName = $Facility
    AND det.ReceiptNumber = @receipt
GROUP BY
        ent.PalletId
    ,   ent.WarehouseSku
    ,   inv.LocationName
    ,   inv.Qty
    ,   inv.LotReferenceOne
ORDER BY ent.WarehouseSku

sample data 在上面的返回中,我需要部分合并的行是4和5。

1 个答案:

答案 0 :(得分:1)

使用子查询创建第二个数据集并加入该子查询-您可以从以下内容推断出适用于您的情况:

首先是子查询:

SELECT
    WarehouseSku,
    SUM(Qty)
FROM
    Inventory
GROUP BY
    WarehouseSku

现在适用于您的查询-插入FROM子句:

...
    LEFT JOIN (
        SELECT
            WarehouseSKU,
            SUM(Qty)
        FROM
            Inventory
        GROUP BY
            WarehouseSKU
    ) AS TotalQty
    ON Warehouse.WarehouseSku = TotalQty.WarehouseSku

没有看到实际的模式DDL,很难知道确切的基数,但是我认为这将为您指明正确的方向。