【发布时间】:2015-05-07 04:08:32
【问题描述】:
我有一个表t,其中包含以下数据:
name | n
------------+---
school | 4
hotel | 2
restaurant | 6
school | 3
school | 5
hotel | 1
当我运行以下查询时,结果有些奇怪。
select name, n,
first_value(n) over (partition by name order by n desc),
last_value(n) over (partition by name order by n)
from t;
name | n | first_value | last_value
------------+---+-------------+------------
hotel | 1 | 2 | 1
hotel | 2 | 2 | 2
restaurant | 6 | 6 | 6
school | 3 | 5 | 3
school | 4 | 5 | 4
school | 5 | 5 | 5
(6 rows)
虽然first_value 可以正常工作,但last_value 却很奇怪。我认为last_value 列的值应该与first_value 的值相同,因为first_value 是按n 降序排列的。
这是 PostgreSQL 的错误还是我遗漏了什么?
PostgreSQL 的版本是:
postgres=# select version();
version
-----------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 9.4.1 on x86_64-apple-darwin14.1.0, compiled by Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn), 64-bit
(1 row)
【问题讨论】:
标签: sql postgresql postgresql-9.4