在SQL Server的同一列中减去2行

时间:2018-08-27 18:46:49

标签: sql sql-server tsql

我试图弄清楚如何从同一列中减去两个行值。 示例:250-161= 8920-20 = 0

QueryExample

BrokenQuantityAdjustment
------------------------
    -161.00
     -89.00
     250.00
     -20.00
      20.00

这是我到目前为止的查询:

SELECT DISTINCT 
    a.SiteID, a.SiteDescription, iv.VendorNumber, iv.VendorName, 
    i.StockNumber, i.StockDescription, mrh.DateReceived, mrh.InvoiceDate, 
    mrh.InvoiceNumber, mrh.DocumentNumber, mrh.ReceivingComment, 
    ia.UnitsPerPack, mri.BrokenQuantityAdjusted, 
    ia.BrokenQuantityAdjustment, ia.BasePackCost, ia.Fee
FROM
    AdmSites AS a 
INNER JOIN
    InvManualReceivingHeader AS mrh ON a.AdmSiteID = mrh.AdmSiteId 
INNER JOIN
    InvAdjustments AS ia ON a.AdmSiteID = ia.AdmSiteID 
                         AND mrh.DateReceived = ia.AdjustmentDate 
                         AND mrh.InvoiceNumber = ia.InvoiceNumber 
INNER JOIN
    InvItems AS i ON ia.InvItemID = i.InvItemID 
INNER JOIN
    InvVendors AS iv ON ia.InvVendorID = iv.InvVendorID 
INNER JOIN
    InvManualReceivingItemsAdjustments AS ria ON mrh.DateReceived = ria.DateReceived 
CROSS JOIN
    InvManualReceivingItems AS mri
WHERE
    (mrh.InvoiceNumber = 'deleted')

我的结果应针对此列:

BrokenQuantityAdjustment
--------------------------
          89
         -89
          0

我不确定如何获得这些结果

2 个答案:

答案 0 :(得分:0)

根据您的快照和说明,我认为您可以将sumgroup by结合使用。

CREATE TABLE T(
  BrokenQuantityAdjustment int,
  BasePackCost float
);

insert into t values (-161.00,0.1615)
insert into t values (-89.00,0.1618)
insert into t values (250.00,0.1615)
insert into t values (-20.00,0.1616)
insert into t values (20.00,0.1616)

查询1

SELECT SUM(BrokenQuantityAdjustment)result
FROM T
GROUP BY BasePackCost

Results

| result |
|--------|
|     89 |
|      0 |
|    -89 |

您可以尝试

;with cte as(
    SELECT DISTINCT 
        a.SiteID, 
        a.SiteDescription, 
        iv.VendorNumber, 
        iv.VendorName, 
        i.StockNumber, 
        i.StockDescription, mrh.DateReceived, mrh.InvoiceDate, 
        mrh.InvoiceNumber, 
        mrh.DocumentNumber, mrh.ReceivingComment, 
        ia.UnitsPerPack, 
        mri.BrokenQuantityAdjusted, 
        ia.BrokenQuantityAdjustment, 
        ia.BasePackCost, 
        ia.Fee
    FROM
        AdmSites AS a 
    INNER JOIN
        InvManualReceivingHeader AS mrh ON a.AdmSiteID = mrh.AdmSiteId 
    INNER JOIN
        InvAdjustments AS ia ON a.AdmSiteID = ia.AdmSiteID 
                             AND mrh.DateReceived = ia.AdjustmentDate 
                             AND mrh.InvoiceNumber = ia.InvoiceNumber 
    INNER JOIN
        InvItems AS i ON ia.InvItemID = i.InvItemID 
    INNER JOIN
        InvVendors AS iv ON ia.InvVendorID = iv.InvVendorID 
    INNER JOIN
        InvManualReceivingItemsAdjustments AS ria ON mrh.DateReceived = ria.DateReceived 
    CROSS JOIN
        InvManualReceivingItems AS mri
    WHERE
        (mrh.InvoiceNumber = 'deleted')
)

select t1.*,t2.result
from cte t1 inner join (
SELECT SUM(BrokenQuantityAdjustment)result,BasePackCost
FROM cte 
GROUP BY BasePackCost
) t2 on t1.BasePackCost = t2.BasePackCost

答案 1 :(得分:0)

如何使用SUM的窗口功能?

例如替换:

ia.BrokenQuantityAdjustment

通过分区的总和:

SUM(ia.BrokenQuantityAdjustment) OVER (PARTITION BY ia.BasePackCost, a.SiteID, a.AdmSiteID, ia.AdjustmentDate, ia.InvoiceNumber, ia.BasePackCost) AS BrokenQuantityAdjustment

虽然不太确定分区中应该有哪些列。