SQL windows 使用 MAX 的函数未给出预期结果
SQL windows function using MAX not giving expected result
所以我正在玩弄 SQL window 函数,遵循使用 AdventureWorks 2012 的在线帮助教程。
我从下面的查询中选择了数据到一个新的 table
SELECT sh.CustomerID AS AccountID,
sd.SalesOrderDetailID AS TransactionID,
sh.OrderDate AS TransactionDate,
sd.LineTotal AS Amount
INTO Transactions
FROM sales.SalesOrderHeader sh INNER JOIN sales.SalesOrderDetail sd on
sh.SalesOrderID = sd.SalesOrderID
然后我运行下面的查询:
select *,
MIN(Amount) OVER(Partition by AccountID order by transactionDate) AS MinOrderForDate
from dbo.transactions
Order by AccountID, transactionID desc
这给了我预期的结果:
AccountID TransactionID TransactionDate Amount MinOrderForDate
11000 63804 2007-11-04 00:00:00.000 53.990000 4.990000
11000 63803 2007-11-04 00:00:00.000 34.990000 4.990000
11000 63802 2007-11-04 00:00:00.000 4.990000 4.990000
11000 63801 2007-11-04 00:00:00.000 28.990000 4.990000
11000 63800 2007-11-04 00:00:00.000 2384.070000 4.990000
11000 38716 2007-07-22 00:00:00.000 21.980000 21.980000
11000 38715 2007-07-22 00:00:00.000 2319.990000 21.980000
11000 449 2005-07-22 00:00:00.000 3399.990000 3399.990000
11001 115673 2008-06-12 00:00:00.000 34.990000 4.990000
11001 115672 2008-06-12 00:00:00.000 8.990000 4.990000
11001 115671 2008-06-12 00:00:00.000 4.990000 4.990000
11001 115670 2008-06-12 00:00:00.000 539.990000 4.990000
11001 38639 2007-07-20 00:00:00.000 8.990000 4.990000
11001 38638 2007-07-20 00:00:00.000 53.990000 4.990000
11001 38637 2007-07-20 00:00:00.000 9.990000 4.990000
11001 38636 2007-07-20 00:00:00.000 4.990000 4.990000
11001 38635 2007-07-20 00:00:00.000 21.980000 4.990000
11001 38634 2007-07-20 00:00:00.000 2319.990000 4.990000
11001 423 2005-07-18 00:00:00.000 3374.990000 3374.990000
但是,当我将 MIN 替换为 MAX 时,我一直在获取帐户的整个数据 运行ge 的 MAX 值,但我不明白为什么?
select *,
MAX(Amount) OVER(Partition by AccountID order by transactionDate) AS MaxOrderForDate
from dbo.transactions
Order by AccountID, transactionID desc
AccountID TransactionID TransactionDate Amount MaxOrderForDate
11000 63804 2007-11-04 00:00:00.000 53.990000 3399.990000
11000 63803 2007-11-04 00:00:00.000 34.990000 3399.990000
11000 63802 2007-11-04 00:00:00.000 4.990000 3399.990000
11000 63801 2007-11-04 00:00:00.000 28.990000 3399.990000
11000 63800 2007-11-04 00:00:00.000 2384.070000 3399.990000
11000 38716 2007-07-22 00:00:00.000 21.980000 3399.990000
11000 38715 2007-07-22 00:00:00.000 2319.990000 3399.990000
11000 449 2005-07-22 00:00:00.000 3399.990000 3399.990000
11001 115673 2008-06-12 00:00:00.000 34.990000 3374.990000
11001 115672 2008-06-12 00:00:00.000 8.990000 3374.990000
11001 115671 2008-06-12 00:00:00.000 4.990000 3374.990000
11001 115670 2008-06-12 00:00:00.000 539.990000 3374.990000
11001 38639 2007-07-20 00:00:00.000 8.990000 3374.990000
11001 38638 2007-07-20 00:00:00.000 53.990000 3374.990000
11001 38637 2007-07-20 00:00:00.000 9.990000 3374.990000
11001 38636 2007-07-20 00:00:00.000 4.990000 3374.990000
11001 38635 2007-07-20 00:00:00.000 21.980000 3374.990000
11001 38634 2007-07-20 00:00:00.000 2319.990000 3374.990000
11001 423 2005-07-18 00:00:00.000 3374.990000 3374.990000
我做错了吗?
您正在按 AccountID 进行分区。它为您提供该 AccountID 的 MAX。对于 AccountID 11000,MAX 为 3399,对于 AccountID 11001,MAX 为 3374。
您的意图不是很明确,但听起来您希望每个 account/transactionDate
分组都有 min/max
。如果是这种情况,您可能不是要将 transactionDate
放在 window 函数的 order by
子句中。
我想你是想这样做:
MIN(Amount) OVER(Partition by AccountID, transactionDate) AS MinOrderForDate
MAX(Amount) OVER(Partition by AccountID, transactionDate) AS MaxOrderForDate
如果您按非唯一列排序,则无法保证结果集。
在您的情况下,结果正是您所要求的,累积 MIN/MAX,由于 ORDER BY
,ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
自动添加到您的查询中。此外,窗口聚合函数中的 ORDER BY
与最终的 ORDER BY
不同,这增加了更多的混淆。
你没有明确说明你想要得到什么。根据列别名,您可能需要 "group" MIN/MAX:
MIN(Amount) OVER(Partition by AccountID, transactionDate) AS MinOrderForDate
所以我正在玩弄 SQL window 函数,遵循使用 AdventureWorks 2012 的在线帮助教程。
我从下面的查询中选择了数据到一个新的 table
SELECT sh.CustomerID AS AccountID,
sd.SalesOrderDetailID AS TransactionID,
sh.OrderDate AS TransactionDate,
sd.LineTotal AS Amount
INTO Transactions
FROM sales.SalesOrderHeader sh INNER JOIN sales.SalesOrderDetail sd on
sh.SalesOrderID = sd.SalesOrderID
然后我运行下面的查询:
select *,
MIN(Amount) OVER(Partition by AccountID order by transactionDate) AS MinOrderForDate
from dbo.transactions
Order by AccountID, transactionID desc
这给了我预期的结果:
AccountID TransactionID TransactionDate Amount MinOrderForDate
11000 63804 2007-11-04 00:00:00.000 53.990000 4.990000
11000 63803 2007-11-04 00:00:00.000 34.990000 4.990000
11000 63802 2007-11-04 00:00:00.000 4.990000 4.990000
11000 63801 2007-11-04 00:00:00.000 28.990000 4.990000
11000 63800 2007-11-04 00:00:00.000 2384.070000 4.990000
11000 38716 2007-07-22 00:00:00.000 21.980000 21.980000
11000 38715 2007-07-22 00:00:00.000 2319.990000 21.980000
11000 449 2005-07-22 00:00:00.000 3399.990000 3399.990000
11001 115673 2008-06-12 00:00:00.000 34.990000 4.990000
11001 115672 2008-06-12 00:00:00.000 8.990000 4.990000
11001 115671 2008-06-12 00:00:00.000 4.990000 4.990000
11001 115670 2008-06-12 00:00:00.000 539.990000 4.990000
11001 38639 2007-07-20 00:00:00.000 8.990000 4.990000
11001 38638 2007-07-20 00:00:00.000 53.990000 4.990000
11001 38637 2007-07-20 00:00:00.000 9.990000 4.990000
11001 38636 2007-07-20 00:00:00.000 4.990000 4.990000
11001 38635 2007-07-20 00:00:00.000 21.980000 4.990000
11001 38634 2007-07-20 00:00:00.000 2319.990000 4.990000
11001 423 2005-07-18 00:00:00.000 3374.990000 3374.990000
但是,当我将 MIN 替换为 MAX 时,我一直在获取帐户的整个数据 运行ge 的 MAX 值,但我不明白为什么?
select *,
MAX(Amount) OVER(Partition by AccountID order by transactionDate) AS MaxOrderForDate
from dbo.transactions
Order by AccountID, transactionID desc
AccountID TransactionID TransactionDate Amount MaxOrderForDate
11000 63804 2007-11-04 00:00:00.000 53.990000 3399.990000
11000 63803 2007-11-04 00:00:00.000 34.990000 3399.990000
11000 63802 2007-11-04 00:00:00.000 4.990000 3399.990000
11000 63801 2007-11-04 00:00:00.000 28.990000 3399.990000
11000 63800 2007-11-04 00:00:00.000 2384.070000 3399.990000
11000 38716 2007-07-22 00:00:00.000 21.980000 3399.990000
11000 38715 2007-07-22 00:00:00.000 2319.990000 3399.990000
11000 449 2005-07-22 00:00:00.000 3399.990000 3399.990000
11001 115673 2008-06-12 00:00:00.000 34.990000 3374.990000
11001 115672 2008-06-12 00:00:00.000 8.990000 3374.990000
11001 115671 2008-06-12 00:00:00.000 4.990000 3374.990000
11001 115670 2008-06-12 00:00:00.000 539.990000 3374.990000
11001 38639 2007-07-20 00:00:00.000 8.990000 3374.990000
11001 38638 2007-07-20 00:00:00.000 53.990000 3374.990000
11001 38637 2007-07-20 00:00:00.000 9.990000 3374.990000
11001 38636 2007-07-20 00:00:00.000 4.990000 3374.990000
11001 38635 2007-07-20 00:00:00.000 21.980000 3374.990000
11001 38634 2007-07-20 00:00:00.000 2319.990000 3374.990000
11001 423 2005-07-18 00:00:00.000 3374.990000 3374.990000
我做错了吗?
您正在按 AccountID 进行分区。它为您提供该 AccountID 的 MAX。对于 AccountID 11000,MAX 为 3399,对于 AccountID 11001,MAX 为 3374。
您的意图不是很明确,但听起来您希望每个 account/transactionDate
分组都有 min/max
。如果是这种情况,您可能不是要将 transactionDate
放在 window 函数的 order by
子句中。
我想你是想这样做:
MIN(Amount) OVER(Partition by AccountID, transactionDate) AS MinOrderForDate
MAX(Amount) OVER(Partition by AccountID, transactionDate) AS MaxOrderForDate
如果您按非唯一列排序,则无法保证结果集。
在您的情况下,结果正是您所要求的,累积 MIN/MAX,由于 ORDER BY
,ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
自动添加到您的查询中。此外,窗口聚合函数中的 ORDER BY
与最终的 ORDER BY
不同,这增加了更多的混淆。
你没有明确说明你想要得到什么。根据列别名,您可能需要 "group" MIN/MAX:
MIN(Amount) OVER(Partition by AccountID, transactionDate) AS MinOrderForDate