人大金仓数据库KingbaseES group by子句
group by、人大金仓、KingbaseES。
关键字:group by、人大金仓、KingbaseES
GROUP BY
语法
GROUP BY { expression | {ROLLUP | CUBE} ({expression | (expression_list)}[, …]) | GROUPING SETS ({expression | (expression_list)}[, …] | {ROLLUP | CUBE} ({expression | (expression_list)}[, …])}) } [, … ] [HAVING condition]
expression表示表达式,可以是列名,投影列别名,投影列位置(从1开始),jsonb列的属性,数字常量,字符串常量,null等或者这几种元素的组合。可选用的操作符包括数学运算操作符,字符串连接操作符和比较操作符。
GROUPING SETS表示多个分组集合。如GROUPING SETS((c1,c2,c3), (c1,c2), ())表示对表按(c1,c2,c3)分组,同时按(c1,c2)分组,按()(表示只有一个分组)分组,将这三个分组结果按顺序放一起。
ROLLUP表示表达式列表分组归纳。如ROLLUP (c1,c2,c3)相当于GROUPING SETS((c1,c2,c3),( c1,c2),(c1),())。
CUBE表示表达式列表分组及其可能的子集分组。如CUBE (c1,c2,c3)相当于GROUPING SETS((c1,c2,c3),( c1,c2),(c1,c3),(c2,c3),(c1),(c2),(c3)())。
expression_list表示表达式,由多个表达式通过‘,’连接起来。
规则
Group by子句:
- 列形式支持投影列别名。
- 列形式支持投影列位置。
- 列类型支持组合类型,blob,long,long raw,blob,clob,nclob,array等类型。
- 列表达式不支持json属性,且在json属性的表示方式与Oracle不同。
- 列表达式为单个整数常量(包括0,-0,负整数):将其看做常量,按一个组处理。
- 列表达式支持单个小数常量(包括负小数常量),将其看做常量,按一个组处理。
- 列表达式支持单个字符串常量,将其看做常量,按一个组处理。
- 列表达式支持单个null,将其看做常量,按一个组处理。
- 列表达式支持比较表达式。
测试用例
表结构构造:
drop table t1;
create table t1(id int, num number, name varchar(20), dat date);
insert into t1 select rownum id, rownum+0.5 num, 'row'||rownum name, '2022-10-'||rownum from dual connect by level <=7;
insert into t1 values(8,null,null,null);
测试语句:
set group_by_int_pos = true;
show group_by_int_pos;
select * from t1 group by 1;
select min(num),min(name),count(*) from t1 group by 1;
select min(num),min(name),count(*) from t1 group by 5;
select min(num),min(name),count(*) from t1 group by 0;
select min(num),min(name),count(*) from t1 group by -0;
select min(num),min(name),count(*) from t1 group by -5;
select min(num),min(name),count(*) from t1 group by 1+5*2+0-5;
测试结果:
测试语句:
set group_by_int_pos = false;
show group_by_int_pos;
select * from t1 group by 1;
select min(num),min(name),count(*) from t1 group by 1;
select min(num),min(name),count(*) from t1 group by 5;
select min(num),min(name),count(*) from t1 group by 0;
select min(num),min(name),count(*) from t1 group by -0;
select min(num),min(name),count(*) from t1 group by -5;
select min(num),min(name),count(*) from t1 group by 1+5*2+0-5;
测试结果:
测试结论:
KES中,Group by子句中
- 列表达式为单个整数常量时,select列表中只能出现常量和聚集函数,不能出现表的列。
- 列表达式为单个整数常量:若group_by_int_pos为true时,整数被单做投影列位置,按投影列进行分组,位置错误则报错;若group_by_int_pos为fanlse时整数被看做常量,只有一个分组。
- 整数常量表达式(不包含表的列)被看做常量,只有一个分组。
(2)小数常量
表结构构造:
drop table t1;
create table t1(id int, num number, name varchar(20), dat date);
insert into t1 select rownum id, rownum+0.5 num, 'row'||rownum name, '2022-10-'||rownum from dual connect by level <=7;
insert into t1 values(8,null,null,null);
测试语句:
select min(num),min(name),count(*) from t1 group by 1.3;
select min(num),min(name),count(*) from t1 group by 5.6;
select min(num),min(name),count(*) from t1 group by 0.00;
select min(num),min(name),count(*) from t1 group by -0.00;
select min(num),min(name),count(*) from t1 group by -5.6;
select min(num),min(name),count(*) from t1 group by 1.3+5.6*2+0.00-5.6;
测试结果:
测试结论:
KES中,Group by子句中
- 单个小数常量被看做常量,只有一个分组。
- 小数常量表达式(不包含表的列)被看做常量,只有一个分组。
(3)字符串常量
表结构构造:
drop table t1;
create table t1(id int, num number, name varchar(20), dat date);
insert into t1 select rownum id, rownum+0.5 num, 'row'||rownum name, '2022-10-'||rownum from dual connect by level <=7;
insert into t1 values(8,null,null,null);
测试语句:
select min(num),min(name),count(*) from t1 group by 'test';
select min(num),min(name),count(*) from t1 group by 'unknown';
select min(num),min(name),count(*) from t1 group by 'test'||'unknown';
测试结果:
测试结论:
KES中,Group by子句中
- 单个字符串常量被看做常量,只有一个分组。
- 字符串常量表达式(不包含表的列)被看做常量,只有一个分组。
(4) null
表结构构造:
drop table t1;
create table t1(id int, num number, name varchar(20), dat date);
insert into t1 select rownum id, rownum+0.5 num, 'row'||rownum name, '2022-10-'||rownum from dual connect by level <=7;
insert into t1 values(8,null,null,null);
测试语句:
select min(num),min(name),count(*) from t1 group by null;
select min(num),min(name),count(*) from t1 group by null+1.3;
select min(num),min(name),count(*) from t1 group by null||'test';
测试结果:
测试结论:
KES中,Group by子句中
- null被看做常量,只有一个分组。
- null表达式(不包含表的列)被看做常量,只有一个分组。
更多推荐
所有评论(0)