通过语句按选择分区,我很难理解这一点。我读到了有关但不了解。使用的好处是什么?
SELECT MIN(Field) OVER (PARTITION BY OtherField) as Value
FROM MYTABLE
您能否简要描述一下,何时使用?为什么?写这样的东西有什么好处和成就目标
答案 0 :(得分:0)
OVER (PARTITION BY OtherField)
是窗口函数。此外,主要思想是按分区分组,而不会减少所选表行的数量。
通常,窗口功能将比联接/聚合解决方案更快。这是一个非常简单的情况,因此性能可能基本相同。
让我展示一个例子:
CREATE TABLE dbo.Duration (startDate datetime2, endDate datetime2);
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-06 16:42:09', '2007-05-07 11:10:08');
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-07 08:30:09', '2007-05-07 12:12:43');
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-07 11:35:09', '2007-05-07 17:13:39');
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-07 08:30:10', '2007-05-07 12:12:43');
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-07 08:30:11', '2007-05-07 12:12:43');
INSERT INTO dbo.Duration (startDate, endDate) VALUES ('2007-05-07 08:30:12', '2007-05-07 12:12:43');
现在,我们可以按字段EndDate
进行分区,并获取startDate MIN()
EndDate`的in the partition of
:
SELECT
MIN(dr.startDate) OVER (PARTITION BY dr.endDate ) MinDateByPartitionOfEndDate
, dr.endDate
FROM dbo.Duration dr
请特别注意2007-05-07 12:12:43
及其值2007-05-07 08:30:09.0000000
。因此我们没有减少查询结果,但是通过所需字段获得了每个分区的MIN
值。
输出:
MinDateByPartitionOfEndDate endDate
2007-05-06 16:42:09.0000000 2007-05-07 11:10:08.0000000
2007-05-07 08:30:09.0000000 2007-05-07 12:12:43.0000000
2007-05-07 08:30:09.0000000 2007-05-07 12:12:43.0000000
2007-05-07 08:30:09.0000000 2007-05-07 12:12:43.0000000
2007-05-07 08:30:09.0000000 2007-05-07 12:12:43.0000000
2007-05-07 11:35:09.0000000 2007-05-07 17:13:39.0000000
2007-05-07 11:35:09.0000000 2007-05-07 17:13:39.0000000
然后通过GROUP BY
(减少查询结果)查询来了解GROUP BY
和PARTITION BY
之间的主要区别:
SELECT
MIN(dr.startDate) MinDateByGroupBy
, dr.endDate
FROM dbo.Duration dr
GROUP BY dr.endDate
输出:
MinDateByGroupBy endDate
2007-05-06 16:42:09.0000000 2007-05-07 11:10:08.0000000
2007-05-07 08:30:09.0000000 2007-05-07 12:12:43.0000000
2007-05-07 11:35:09.0000000 2007-05-07 17:13:39.0000000