sql - Informix 窗口函数范围子句的行为类似于行子句

标签 sql informix window-functions

考虑 Informix 中的这个语句:

SELECT 
  a, 
  b,
  sum(a) over (order by b) "no frame",
  sum(a) over (order by b range between unbounded preceding and current row) "range",
  sum(a) over (order by b rows between unbounded preceding and current row) "rows"
FROM TABLE(SET{row(1, 1), row(2, 1), row(3, 2), row(4, 2)}) AS t(a, b);

令人惊讶的是,它对 rangerows 产生了相同的结果:

a |b |no frame |range |rows |
--|--|---------|------|-----|
1 |1 |3        |1     |1    |
2 |1 |3        |3     |3    |
3 |2 |10       |6     |6    |
4 |2 |10       |10    |10   |

这与我习惯的任何 RDBMS(包括 PostgreSQL)的工作方式完全不同:

SELECT 
  a, 
  b,
  sum(a) over (order by b) "no frame",
  sum(a) over (order by b range between unbounded preceding and current row) "range",
  sum(a) over (order by b rows between unbounded preceding and current row) "rows"
FROM (values(1, 1), (2, 1), (3, 2), (4, 2)) AS t(a, b);

...产生预期的

a |b |no frame |range |rows |
--|--|---------|------|-----|
1 |1 |3        |3     |1    |
2 |1 |3        |3     |3    |
3 |2 |10       |10    |6    |
4 |2 |10       |10    |10   |

我遗漏了 Informix 和标准 SQL 之间的细微差别,还是一个错误?我正在使用 official docker image ,对应于目前的 Informix 12.10。

最佳答案

这是 documented行为:

If you specify an ORDER clause but no window frame clause for a window aggregation function, then by default, all rows that precede the current row and the current row are returned, which is equivalent to the following window frame specification:

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

我相信当不存在窗口子句时,标准是 RANGE。我很确定这是大多数其他数据库的默认设置。

关于sql - Informix 窗口函数范围子句的行为类似于行子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188186/

相关文章:

sql - 查找窗口中 n 个类别之间的变化之前的最大值(类别之间有 m>n 个变化)

php - 如何获得 SQL 中重复项求和的最佳项?

mysql - 使用不同表的计数进行计划和更新

Informix:如何使用 dbaccess 获取表内容和列名?

c# - 什么时候可以使用 ClearAllPools 方法?

MySQL 表值与当前日期和上一个日期的差异

sql - GBQ 窗函数 AND 算术运算

sql - postgresql:从 Char Varying 字段中提取多个值

PHP + MySQL 多数据库网站到带前缀的单数据库网站。

mysql - 连接 MySQL 和 Informix 表