PostgreSQL error in window function on partition?

I have a table tthat has the following data:

    name    | n
------------+---
 school     | 4
 hotel      | 2
 restaurant | 6
 school     | 3
 school     | 5
 hotel      | 1

When I run the following query, the result is somewhat odd.

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)

Although it first_valueworks the way I expected, it last_valueworks weird. I think the column values last_valueshould be the same as first_valuebecause they first_valueare nin descending order.

Is this a PostgreSQL error or am I missing something?

PostgreSQL Version:

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)
+4
source share
1 answer

, . first_value() last_value() , . , , , frame_clause. , first_value(), last_value() range between unbounded preceding and unbounded following WINDOW, :

select name, n,
       first_value(n) over (partition by name order by n desc),
       last_value(n) over (partition by name order by n
         range between unbounded preceding and unbounded following)
from t;

, . (), , - .

+9

All Articles