summaryrefslogtreecommitdiff
path: root/mysql-test/t/win_first_last_value.test
blob: 0a90a98a5d47f42c074a5a3dd59c1ea5d9cc999b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
create table t1 (
  pk int primary key,
  a int,
  b int,
  c char(10),
  d decimal(10, 3),
  e real
);

insert into t1 values
( 1, 0, 1,    'one',    0.1,  0.001),
( 2, 0, 2,    'two',    0.2,  0.002),
( 3, 0, 3,    'three',  0.3,  0.003),
( 4, 1, 2,    'three',  0.4,  0.004),
( 5, 1, 1,    'two',    0.5,  0.005),
( 6, 1, 1,    'one',    0.6,  0.006),
( 7, 2, NULL, 'n_one',  0.5,  0.007),
( 8, 2, 1,    'n_two',  NULL, 0.008),
( 9, 2, 2,    NULL,     0.7,  0.009),
(10, 2, 0,    'n_four', 0.8,  0.010),
(11, 2, 10,   NULL,     0.9,  NULL);

select pk, first_value(pk) over (order by pk),
           last_value(pk) over (order by pk),
           first_value(pk) over (order by pk desc),
           last_value(pk) over (order by pk desc)
from t1
order by pk desc;

select pk,
       first_value(pk) over (order by pk
                             RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
       last_value(pk) over (order by pk
                            RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
       first_value(pk) over (order by pk desc
                             RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
       last_value(pk) over (order by pk desc
                            RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
from t1
order by pk;

select pk,
       first_value(pk) over (order by pk desc),
       last_value(pk) over (order by pk desc)
from t1;

select pk, a, b, c, d, e,
       first_value(b) over (partition by a order by pk) as fst_b,
       last_value(b) over (partition by a order by pk) as lst_b,
       first_value(c) over (partition by a order by pk) as fst_c,
       last_value(c) over (partition by a order by pk) as lst_c,
       first_value(d) over (partition by a order by pk) as fst_d,
       last_value(d) over (partition by a order by pk) as lst_d,
       first_value(e) over (partition by a order by pk) as fst_e,
       last_value(e) over (partition by a order by pk) as lst_e
from t1;

drop table t1;

--echo #
--echo # MDEV-11746: Wrong result upon using FIRST_VALUE with a window frame
--echo #
create table t1 (i int);
insert into t1 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

select i,
       first_value(i) OVER (order by i rows between CURRENT ROW and 1 FOLLOWING) as fst_1f,
       last_value(i) OVER (order by i rows between CURRENT ROW and 1 FOLLOWING) as last_1f,
       first_value(i) OVER (order by i rows between 1 PRECEDING AND 1 FOLLOWING) as fst_1p1f,
       last_value(i) OVER (order by i rows between 1 PRECEDING AND 1 FOLLOWING) as fst_1p1f,
       first_value(i) OVER (order by i rows between 2 PRECEDING AND 1 PRECEDING) as fst_2p1p,
       last_value(i) OVER (order by i rows between 2 PRECEDING AND 1 PRECEDING) as fst_2p1p,
       first_value(i) OVER (order by i rows between 1 FOLLOWING AND 2 FOLLOWING) as fst_1f2f,
       last_value(i) OVER (order by i rows between 1 FOLLOWING AND 2 FOLLOWING) as fst_1f2f
from t1;

drop table t1;

--echo #
--echo # MDEV-12861 FIRST_VALUE() does not preserve the exact data type
--echo #

CREATE TABLE t1 (a INT, b INT, c FLOAT);
INSERT INTO t1 VALUES (1,1,1),(1,2,2),(2,1,1),(2,2,2);
CREATE TABLE t2 AS SELECT a, FIRST_VALUE(b) OVER(), FIRST_VALUE(c) OVER() FROM t1 GROUP BY a;
SHOW CREATE TABLE t2;
DROP TABLE t2,t1;