DEV Community

Michael
Michael

Posted on

Using last_value in GBase 8a: Reading the Last Value of a Window

last_value in the China-domestically developed GBase 8a MPP database from GBASE is the counterpart of first_value: it returns the last value of each window, taken up to the current row. This post covers the syntax and a set of runnable examples.

Syntax

last_value(XXX) OVER([PARTITION BY col_name1,col_name2,…] ORDER BY col_name1 [asc/desc], col_name2 [asc/desc],…)
Enter fullscreen mode Exit fullscreen mode

Notes

  • Each window (or all rows as one window when PARTITION BY is omitted) is sorted on the given columns, then the last row up to the current row is returned. Without ORDER BY, the default order is used.
  • The argument can be a column or an expression.
  • Watch how NULL sorts. If you are not sure, pre-process the column first, for example with the nvl function.

Examples

Sample data

gbase> create table td(id int, type int,val decimal(20,3));
Query OK, 0 rows affected (Elapsed: 00:00:00.17)

gbase> insert into td values(1,1,111),(1,1,222),(2,1,333),(2,2,444),(3,2,555),(3,2,666),(4,2,777);
Query OK, 7 rows affected (Elapsed: 00:00:00.12)
Records: 7  Duplicates: 0  Warnings: 0

gbase> insert into td values(5,1,null),(50,2,null);
Query OK, 2 rows affected (Elapsed: 00:00:00.09)
Records: 2  Duplicates: 0  Warnings: 0

gbase> select * from td;
+------+------+---------+
| id   | type | val     |
+------+------+---------+
|    1 |    1 | 111.000 |
|    1 |    1 | 222.000 |
|    2 |    1 | 333.000 |
|    2 |    2 | 444.000 |
|    3 |    2 | 555.000 |
|    3 |    2 | 666.000 |
|    4 |    2 | 777.000 |
|    5 |    1 |    NULL |
|   50 |    2 |    NULL |
+------+------+---------+
9 rows in set (Elapsed: 00:00:00.03)
Enter fullscreen mode Exit fullscreen mode

Full window usage

Sorting with order by id and with order by id desc gives different "last" rows, and NULL values affect the result in both cases.

gbase> select *,last_value(val)over(partition by type order by id) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 222.000 |   111.000 |
|    1 |    1 | 111.000 |   111.000 |
|    2 |    1 | 333.000 |   333.000 |
|    5 |    1 |    NULL |      NULL |
|    2 |    2 | 444.000 |   444.000 |
|    3 |    2 | 666.000 |   555.000 |
|    3 |    2 | 555.000 |   555.000 |
|    4 |    2 | 777.000 |   777.000 |
|   50 |    2 |    NULL |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.35)

gbase> select *,last_value(val)over(partition by type order by id desc) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    5 |    1 |    NULL |      NULL |
|    2 |    1 | 333.000 |   333.000 |
|    1 |    1 | 222.000 |   111.000 |
|    1 |    1 | 111.000 |   111.000 |
|   50 |    2 |    NULL |      NULL |
|    4 |    2 | 777.000 |   777.000 |
|    3 |    2 | 555.000 |   666.000 |
|    3 |    2 | 666.000 |   666.000 |
|    2 |    2 | 444.000 |   444.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.34)
Enter fullscreen mode Exit fullscreen mode

Without PARTITION BY

All rows take part in a single sort.

gbase> select *,last_value(val)over(order by id) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 222.000 |   111.000 |
|    1 |    1 | 111.000 |   111.000 |
|    2 |    2 | 444.000 |   333.000 |
|    2 |    1 | 333.000 |   333.000 |
|    3 |    2 | 555.000 |   666.000 |
|    3 |    2 | 666.000 |   666.000 |
|    4 |    2 | 777.000 |   777.000 |
|    5 |    1 |    NULL |      NULL |
|   50 |    2 |    NULL |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.07)

gbase> select *,last_value(val)over(order by id desc) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|   50 |    2 |    NULL |      NULL |
|    5 |    1 |    NULL |      NULL |
|    4 |    2 | 777.000 |   777.000 |
|    3 |    2 | 666.000 |   555.000 |
|    3 |    2 | 555.000 |   555.000 |
|    2 |    1 | 333.000 |   444.000 |
|    2 |    2 | 444.000 |   444.000 |
|    1 |    1 | 111.000 |   222.000 |
|    1 |    1 | 222.000 |   222.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.09)
Enter fullscreen mode Exit fullscreen mode

PARTITION BY without ORDER BY

Each window keeps its natural order.

gbase> select *,last_value(val)over(partition by type) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 222.000 |   111.000 |
|    2 |    1 | 333.000 |   111.000 |
|    5 |    1 |    NULL |   111.000 |
|    1 |    1 | 111.000 |   111.000 |
|    3 |    2 | 666.000 |      NULL |
|    4 |    2 | 777.000 |      NULL |
|    3 |    2 | 555.000 |      NULL |
|    2 |    2 | 444.000 |      NULL |
|   50 |    2 |    NULL |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.10)
Enter fullscreen mode Exit fullscreen mode

Neither PARTITION BY nor ORDER BY

All rows form one window and the last value in natural order is taken; this form has little practical use.

gbase> select *,last_value(val)over() maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 111.000 |      NULL |
|    1 |    1 | 222.000 |      NULL |
|    2 |    1 | 333.000 |      NULL |
|    2 |    2 | 444.000 |      NULL |
|    3 |    2 | 555.000 |      NULL |
|    3 |    2 | 666.000 |      NULL |
|    4 |    2 | 777.000 |      NULL |
|    5 |    1 |    NULL |      NULL |
|   50 |    2 |    NULL |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.06)
Enter fullscreen mode Exit fullscreen mode

Going forward, last_value is defined relative to the current row, so the ORDER BY direction and NULL placement change the answer row by row. Decide both explicitly in a gbase query, and confirm the plan on a gbase cluster before trusting the numbers in a database report; a window without a deterministic sort is a common source of quiet, hard-to-reproduce errors.

Top comments (0)