0

我正在尝试使用OVER子句返回一行。我的代码如下:

SELECT SUM(Price) OVER (ORDER BY [InvoiceID] ROWS UNBOUNDED PRECEDING) 
FROM dbo.Sales 
WHERE InvoiceID = 3427

我想返回:

InvoiceID Price

3427      15.00

相反,返回的是:

InvoiceID    Price

3427    5.00

3427    10.00

3427     15.00

我如何只得到一排?

4

2 回答 2

1

不知道为什么要使用OVER()子句。似乎您只需要:

SELECT InvoiceID, Price = SUM(Price)
  FROM dbo.Sales 
  WHERE InvoiceID = 3427  -- maybe you want more than just this one?
                          -- if so, leave out the WHERE clause
  GROUP BY InvoiceID;
于 2013-10-16T03:07:12.080 回答
0

实际上,OVER 子句中的窗口规范具有三个主要部分:分区、排序和框架。窗口框架子句 (ROWS BETWEEN AND ) 过滤两个指定分隔符之间的窗口分区中的一个框架或行的子集。在您的查询中,没有正确指定框架,它应该位于分区的开头(即 UNBOUNDED PRECEDING)和当前行(CURRENT ROW)之间。以下查询应该有帮助:

SELECT SUM(Price) OVER (ORDER BY [InvoiceID] ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW) 
FROM dbo.Sales 
WHERE InvoiceID = 3427
于 2013-10-16T05:26:51.187 回答