2016-02-05 12:24:35 +01:00
drop table if exists t1,t2;
2016-02-06 23:06:56 +01:00
# ########################################################################
# # Parser tests
# ########################################################################
2016-02-05 23:53:17 +01:00
#
# Check what happens when one attempts to use window function without OVER clause
create table t1 (a int, b int);
insert into t1 values (1,1),(2,2);
select row_number() from t1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from t1' at line 1
select rank() from t1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from t1' at line 1
2016-02-06 23:06:56 +01:00
# Attempt to use window function in the WHERE clause
select * from t1 where 1=rank() over (order by a);
ERROR HY000: Invalid use of group function
select * from t1 where 1>row_number() over (partition by b order by a);
ERROR HY000: Invalid use of group function
2016-02-05 23:53:17 +01:00
drop table t1;
2016-02-06 23:06:56 +01:00
# ########################################################################
# # Functionality tests
# ########################################################################
2016-02-05 23:53:17 +01:00
#
2016-02-06 23:06:56 +01:00
# Check if ROW_NUMBER() works in basic cases
2016-02-05 12:24:35 +01:00
create table t1(a int, b int, x char(32));
insert into t1 values (2, 10, 'xx');
insert into t1 values (2, 10, 'zz');
insert into t1 values (2, 20, 'yy');
insert into t1 values (3, 10, 'xxx');
insert into t1 values (3, 20, 'vvv');
select a, row_number() over (partition by a order by b) from t1;
a row_number() over (partition by a order by b)
2016-02-14 19:00:05 +01:00
2 1
2016-02-15 16:46:02 +01:00
2 2
2016-02-05 12:24:35 +01:00
2 3
3 1
3 2
select a, b, x, row_number() over (partition by a order by x) from t1;
a b x row_number() over (partition by a order by x)
2 10 xx 1
2 10 zz 3
2 20 yy 2
3 10 xxx 2
3 20 vvv 1
drop table t1;
create table t1 (pk int primary key, a int, b int);
insert into t1 values
(1, 10, 22),
(2, 11, 21),
(3, 12, 20),
(4, 13, 19),
(5, 14, 18);
select
pk, a, b,
row_number() over (order by a),
row_number() over (order by b)
from t1;
pk a b row_number() over (order by a) row_number() over (order by b)
1 10 22 1 5
2 11 21 2 4
3 12 20 3 3
4 13 19 4 2
5 14 18 5 1
drop table t1;
2016-02-05 23:53:17 +01:00
#
# Try RANK() function
#
create table t2 (
pk int primary key,
a int
);
insert into t2 values
( 1 , 0),
( 2 , 0),
( 3 , 1),
( 4 , 1),
( 8 , 2),
( 5 , 2),
( 6 , 2),
( 7 , 2),
( 9 , 4),
(10 , 4);
select pk, a, rank() over (order by a) from t2;
pk a rank() over (order by a)
1 0 1
2 0 1
3 1 3
4 1 3
8 2 5
5 2 5
6 2 5
7 2 5
9 4 9
10 4 9
drop table t2;