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;
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) 111111111 101101110 919119 818118 717117 616116 515115 414114 313113 212112 111111
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;
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) 1111111 2111111 3111111 4111111 5111111 6111111 7111111 8111111 9111111 10111111 11111111
select pk,
first_value(pk) over (order by pk desc),
last_value(pk) over (order by pk desc)
from t1;
pk first_value(pk) over (order by pk desc) last_value(pk) over (order by pk desc) 111111 101110 9119 8118 7117 6116 5115 4114 3113 2112 1111
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;
pk a b c d e fst_b lst_b fst_c lst_c fst_d lst_d fst_e lst_e 101 one 0.1000.00111 one one 0.1000.1000.0010.001 202 two 0.2000.00212 one two 0.1000.2000.0010.002 303 three 0.3000.00313 one three 0.1000.3000.0010.003 412 three 0.4000.00422 three three 0.4000.4000.0040.004 511 two 0.5000.00521 three two 0.4000.5000.0040.005 611 one 0.6000.00621 three one 0.4000.6000.0040.006 72 NULL n_one 0.5000.007 NULL NULL n_one n_one 0.5000.5000.0070.007 821 n_two NULL 0.008 NULL 1 n_one n_two 0.500 NULL 0.0070.008 922 NULL 0.7000.009 NULL 2 n_one NULL 0.5000.7000.0070.009 1020 n_four 0.8000.01 NULL 0 n_one n_four 0.5000.8000.0070.01 11210 NULL 0.900 NULL NULL 10 n_one NULL 0.5000.9000.007 NULL
drop table t1;
#
# MDEV-11746: Wrong result upon using FIRST_VALUE with a window frame
#
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 and1 FOLLOWING) as fst_1f,
last_value(i) OVER (order by i rows between CURRENT ROW and1 FOLLOWING) as last_1f,
first_value(i) OVER (order by i rows between 1 PRECEDING AND1 FOLLOWING) as fst_1p1f,
last_value(i) OVER (order by i rows between 1 PRECEDING AND1 FOLLOWING) as fst_1p1f,
first_value(i) OVER (order by i rows between 2 PRECEDING AND1 PRECEDING) as fst_2p1p,
last_value(i) OVER (order by i rows between 2 PRECEDING AND1 PRECEDING) as fst_2p1p,
first_value(i) OVER (order by i rows between 1 FOLLOWING AND2 FOLLOWING) as fst_1f2f,
last_value(i) OVER (order by i rows between 1 FOLLOWING AND2 FOLLOWING) as fst_1f2f
from t1;
i fst_1f last_1f fst_1p1f fst_1p1f fst_2p1p fst_2p1p fst_1f2f fst_1f2f 11212 NULL NULL 23 223131134 334241245 445352356 556463467 667574578 778685689 8897967910 9910810781010 10101091089 NULL NULL
drop table t1;
#
# MDEV-12861 FIRST_VALUE() does not preserve the exact data type
#
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;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`FIRST_VALUE(b) OVER()` int(11) DEFAULT NULL,
`FIRST_VALUE(c) OVER()` float DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2,t1;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-10-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.