在sql中获取总运行

时间:2016-08-27 08:29:00

标签: sql sql-server sql-server-2012

如何从下面的查询计算运行总计

查询

SELECT
  dbo.PurchaseInvoices.PurchaseInvoiceNo as [Invoice No] ,
  dbo.PurchaseInvoices.PurchaseInvoiceDate as [Invoice Date],
  Suppliers.SupplierName,
  dbo.Restaurants.Name as [Restaurant Name],
  CONVERT(decimal(9, 2), SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) AS Debit
FROM dbo.PurchaseOrders
INNER JOIN dbo.Requisitions
  ON dbo.PurchaseOrders.RequisitionID = dbo.Requisitions.RequisitionID
INNER JOIN dbo.Restaurants
  ON dbo.Requisitions.RestaurantID = dbo.Restaurants.RestaurantID
INNER JOIN dbo.Suppliers
  ON dbo.PurchaseOrders.SupplierID = dbo.Suppliers.SupplierID
INNER JOIN dbo.Categories
  ON dbo.Requisitions.CategoryID = dbo.Categories.CategoryID
INNER JOIN dbo.PurchaseInvoices
  ON dbo.PurchaseOrders.PurchaseOrderID = dbo.PurchaseInvoices.PurchaseInvoiceID
INNER JOIN dbo.RequisitionDetails
  ON dbo.RequisitionDetails.RequisitionID = dbo.Requisitions.RequisitionID
GROUP BY dbo.PurchaseOrders.PurchaseOrderID,
         dbo.Restaurants.Name,
         dbo.PurchaseInvoices.PurchaseInvoiceDate,
         dbo.PurchaseInvoices.PurchaseInvoiceNo,
         Suppliers.SupplierName
ORDER BY dbo.PurchaseInvoices.PurchaseInvoiceNo

查询输出

+------------+--------------+---------------+-----------------+--------+
| Invoice No | Invoice Date | Supplier Name | Restaurant Name |  Debit |
+------------+--------------+---------------+-----------------+--------+
|          1 | 8/26/2016    | supplier1     | restaurant 1    |  92.00 |
|          2 | 8/27/2016    | supplier1     | restaurant 2    |  47.00 |
+------------+--------------+---------------+-----------------+--------+

所需的输出应如下所示

+------------+--------------+---------------+-----------------+--------+---------+
| Invoice No | Invoice Date | Supplier Name | Restaurant Name |  Debit | Balance |
+------------+--------------+---------------+-----------------+--------+---------+
|          1 | 8/26/2016    | supplier1     | restaurant 1    |  92.00 |   92.00 |
|          2 | 8/27/2016    | supplier1     | restaurant 2    |  47.00 |  139.00 |
+------------+--------------+---------------+-----------------+--------+---------+

我怎样才能做到这一点?总计跑步。

2 个答案:

答案 0 :(得分:2)

您可以使用APPLY运算符或Correlated sub-query来查找总计

;with result as 
(
SELECT
  dbo.PurchaseInvoices.PurchaseInvoiceNo as [Invoice No] ,
  dbo.PurchaseInvoices.PurchaseInvoiceDate as [Invoice Date],
  Suppliers.SupplierName,
  dbo.Restaurants.Name as [Restaurant Name],
  CONVERT(decimal(9, 2), SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) AS Debit
FROM dbo.PurchaseOrders
INNER JOIN dbo.Requisitions
  ON dbo.PurchaseOrders.RequisitionID = dbo.Requisitions.RequisitionID
INNER JOIN dbo.Restaurants
  ON dbo.Requisitions.RestaurantID = dbo.Restaurants.RestaurantID
INNER JOIN dbo.Suppliers
  ON dbo.PurchaseOrders.SupplierID = dbo.Suppliers.SupplierID
INNER JOIN dbo.Categories
  ON dbo.Requisitions.CategoryID = dbo.Categories.CategoryID
INNER JOIN dbo.PurchaseInvoices
  ON dbo.PurchaseOrders.PurchaseOrderID = dbo.PurchaseInvoices.PurchaseInvoiceID
INNER JOIN dbo.RequisitionDetails
  ON dbo.RequisitionDetails.RequisitionID = dbo.Requisitions.RequisitionID
GROUP BY dbo.PurchaseOrders.PurchaseOrderID,
         dbo.Restaurants.Name,
         dbo.PurchaseInvoices.PurchaseInvoiceDate,
         dbo.PurchaseInvoices.PurchaseInvoiceNo,
         Suppliers.SupplierName
)
SELECT * 
FROM   result a 
       OUTER apply (SELECT Sum([debit]) 
                    FROM   result b 
                    WHERE  a.[invoice no] >= b.[invoice no]) cs (balance) 

很遗憾,您仍然在Sql Server 2008Sql Server 2012+我们有Sum() Over(Order by)聚合窗口函数来计算运行速度比上述方法更快

更新SQL SERVER 2012+使用此

;with result as
(
SELECT
  dbo.PurchaseInvoices.PurchaseInvoiceNo as [Invoice No] ,
  dbo.PurchaseInvoices.PurchaseInvoiceDate as [Invoice Date],
  Suppliers.SupplierName,
  dbo.Restaurants.Name as [Restaurant Name],
  CONVERT(decimal(9, 2), SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) AS Debit
FROM dbo.PurchaseOrders
INNER JOIN dbo.Requisitions
  ON dbo.PurchaseOrders.RequisitionID = dbo.Requisitions.RequisitionID
INNER JOIN dbo.Restaurants
  ON dbo.Requisitions.RestaurantID = dbo.Restaurants.RestaurantID
INNER JOIN dbo.Suppliers
  ON dbo.PurchaseOrders.SupplierID = dbo.Suppliers.SupplierID
INNER JOIN dbo.Categories
  ON dbo.Requisitions.CategoryID = dbo.Categories.CategoryID
INNER JOIN dbo.PurchaseInvoices
  ON dbo.PurchaseOrders.PurchaseOrderID = dbo.PurchaseInvoices.PurchaseInvoiceID
INNER JOIN dbo.RequisitionDetails
  ON dbo.RequisitionDetails.RequisitionID = dbo.Requisitions.RequisitionID
GROUP BY dbo.PurchaseOrders.PurchaseOrderID,
         dbo.Restaurants.Name,
         dbo.PurchaseInvoices.PurchaseInvoiceDate,
         dbo.PurchaseInvoices.PurchaseInvoiceNo,
         Suppliers.SupplierName
)
SELECT *, sum(Debit) over(Order by [invoice no]) as balance
FROM   result a 

答案 1 :(得分:0)

使用SUM()OVER(ORDER BY ROWS UNBOUNDED PRECEDING AND CURRENT ROW) 请尝试以下选择语句:

SELECT
  dbo.PurchaseInvoices.PurchaseInvoiceNo as [Invoice No] ,
  dbo.PurchaseInvoices.PurchaseInvoiceDate as [Invoice Date],
  Suppliers.SupplierName,
  dbo.Restaurants.Name as [Restaurant Name],
  CONVERT(decimal(9, 2), SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) AS Debit,
  CONVERT(decimal(9,2), SUM(SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) OVER(ORDER BY dbo.PurchaseInvoices.PurchaseInvoiceNo ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) ) AS Balance
FROM ...