2112 lines
73 KiB
Plaintext
2112 lines
73 KiB
Plaintext
drop table if exists t;
|
|
create table t(id int primary key, b varchar(50), c int);
|
|
insert into t values(1, '1ff', NULL), (2, '234.02', 1);
|
|
select id, sum(b) from t group by id;
|
|
id sum(b)
|
|
1 1
|
|
2 234.02
|
|
select sum(b) from t;
|
|
sum(b)
|
|
235.02
|
|
select id, count(c) from t group by id;
|
|
id count(c)
|
|
1 0
|
|
2 1
|
|
drop table if exists t;
|
|
create table t(id int primary key, b float, c float);
|
|
insert into t values(1, 1, 3), (2, 1, 6);
|
|
select sum(b/c) from t group by id;
|
|
sum(b/c)
|
|
0.3333333333333333
|
|
0.16666666666666666
|
|
drop table if exists t;
|
|
create table t(id int primary key, b float, c float, d float);
|
|
insert into t values(1, 1, 3, NULL), (2, 1, NULL, 6), (3, NULL, 1, 2), (4, NULL, NULL, 1), (5, NULL, 2, NULL), (6, 3, NULL, NULL), (7, NULL, NULL, NULL), (8, 1, 2 ,3);
|
|
select count(distinct b, c, d) from t group by id;
|
|
count(distinct b, c, d)
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
1
|
|
select approx_count_distinct( b, c, d) from t group by id order by id;
|
|
approx_count_distinct( b, c, d)
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
0
|
|
1
|
|
drop table if exists t;
|
|
create table t(a int primary key, b varchar(10));
|
|
insert into t value(1, 11),(3, NULL);
|
|
SELECT a, MIN(b), MAX(b) FROM t GROUP BY a;
|
|
a MIN(b) MAX(b)
|
|
1 11 11
|
|
3 NULL NULL
|
|
drop table if exists t;
|
|
create table t (a int, b int, c int, index idx(a, b, c));
|
|
select count(a) from t group by a;
|
|
count(a)
|
|
select count(a) from t;
|
|
count(a)
|
|
0
|
|
insert t values(0,0,0);
|
|
select distinct b from t;
|
|
b
|
|
0
|
|
select count(b) from t group by a;
|
|
count(b)
|
|
1
|
|
insert t values(1,1,1),(3,3,6),(3,2,5),(2,1,4),(1,1,3),(1,1,2);
|
|
select count(a) from t where b>0 group by a, b;
|
|
count(a)
|
|
1
|
|
1
|
|
1
|
|
3
|
|
select count(a) from t where b>0 group by a, b order by a;
|
|
count(a)
|
|
3
|
|
1
|
|
1
|
|
1
|
|
select count(a) from t where b>0 group by a, b order by a limit 1;
|
|
count(a)
|
|
3
|
|
drop table if exists t, tt;
|
|
create table t(a int primary key, b int, c int);
|
|
create table tt(a int primary key, b int, c int);
|
|
insert into t values(1, 1, 1), (2, 1, 1);
|
|
insert into tt values(1, 2, 1);
|
|
select max(a.b), max(b.b) from t a join tt b on a.a = b.a group by a.c;
|
|
max(a.b) max(b.b)
|
|
1 2
|
|
select a, count(b) from (select * from t union all select * from tt) k group by a order by a;
|
|
a count(b)
|
|
1 2
|
|
2 1
|
|
set sql_mode = 'ONLY_FULL_GROUP_BY';
|
|
drop table if exists s;
|
|
create table s(a int);
|
|
select count(a) , date_format(a, '%Y-%m-%d') from s group by date_format(a, '%Y-%m-%d');
|
|
count(a) date_format(a, '%Y-%m-%d')
|
|
select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by date_format(a, '%Y-%m-%d');
|
|
count(a) xx
|
|
select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by xx;
|
|
count(a) xx
|
|
set sql_mode = default;
|
|
drop table if exists t1;
|
|
CREATE TABLE t1 (
|
|
a int(11) DEFAULT NULL,
|
|
b tinyint(4) NOT NULL,
|
|
PRIMARY KEY (b)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
|
|
PARTITION BY RANGE ( b ) (
|
|
PARTITION p0 VALUES LESS THAN (10),
|
|
PARTITION p1 VALUES LESS THAN (20),
|
|
PARTITION p2 VALUES LESS THAN (30),
|
|
PARTITION p3 VALUES LESS THAN (40),
|
|
PARTITION p4 VALUES LESS THAN (MAXVALUE)
|
|
);
|
|
insert into t1 values (0, 0), (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6), (3, 7), (3, 10), (3, 11), (12, 12), (12, 13), (14, 14), (14, 15), (20, 20), (20, 21), (20, 22), (23, 23), (23, 24), (23, 25), (31, 30), (31, 31), (31, 32), (33, 33), (33, 34), (33, 35), (36, 36), (80, 80), (90, 90), (100, 100);
|
|
set @@tidb_opt_agg_push_down = 1;
|
|
select /*+ AGG_TO_COP() */ sum(a), sum(b) from t1 where a < 40 group by a;
|
|
sum(a) sum(b)
|
|
0 0
|
|
24 25
|
|
28 29
|
|
3 6
|
|
36 36
|
|
6 15
|
|
60 63
|
|
69 72
|
|
9 28
|
|
93 93
|
|
99 102
|
|
set @@tidb_opt_agg_push_down = default;
|
|
set sql_mode = 'ONLY_FULL_GROUP_BY';
|
|
drop table if exists t;
|
|
create table t(a int);
|
|
select ((+a+1)) as tmp from t group by tmp;
|
|
tmp
|
|
set @@tidb_opt_agg_push_down = default;
|
|
set sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
|
|
drop table if exists t;
|
|
create table t (c1 int, c2 int, c3 int);
|
|
insert into t values (1,2,3), (2, 3, 1), (3, 1, 2);
|
|
select c1 as c2, c3 from t having c2 = 2;
|
|
c2 c3
|
|
2 1
|
|
select c1 as c2, c3 from t group by c2 having c2 = 2;
|
|
c2 c3
|
|
1 3
|
|
select c1 as c2, c3 from t group by c2 having sum(c2) = 2;
|
|
c2 c3
|
|
1 3
|
|
select c1 as c2, c3 from t group by c3 having sum(c2) = 2;
|
|
c2 c3
|
|
1 3
|
|
select c1 as c2, c3 from t group by c3 having sum(0) + c2 = 2;
|
|
c2 c3
|
|
2 1
|
|
select c1 as a from t having c1 = 1;
|
|
a
|
|
1
|
|
select t.c1 from t having c1 = 1;
|
|
c1
|
|
1
|
|
select a.c1 from t as a having c1 = 1;
|
|
c1
|
|
1
|
|
select c1 as a from t group by c3 having sum(a) = 1;
|
|
a
|
|
1
|
|
select c1 as a from t group by c3 having sum(a) + a = 2;
|
|
a
|
|
1
|
|
select a.c1 as c, a.c1 as d from t as a, t as b having c1 = 1 limit 1;
|
|
c d
|
|
1 1
|
|
select sum(c1) as s from t group by c1 having sum(c1) order by s;
|
|
s
|
|
1
|
|
2
|
|
3
|
|
select sum(c1) - 1 as s from t group by c1 having sum(c1) - 1 order by s;
|
|
s
|
|
1
|
|
2
|
|
select 1 from t group by c1 having sum(abs(c2 + c3)) = c1;
|
|
1
|
|
1
|
|
set @@tidb_opt_agg_push_down = default;
|
|
drop table if exists t;
|
|
create table t(a int primary key, b int);
|
|
select min(a), min(a) from t;
|
|
min(a) min(a)
|
|
NULL NULL
|
|
insert into t values(1, -1), (2, -2), (3, 1), (4, NULL);
|
|
select max(a) from t;
|
|
max(a)
|
|
4
|
|
select min(b) from t;
|
|
min(b)
|
|
-2
|
|
select max(b*b) from t;
|
|
max(b*b)
|
|
4
|
|
select min(b*b) from t;
|
|
min(b*b)
|
|
1
|
|
select group_concat(b, b) from t group by a;
|
|
group_concat(b, b)
|
|
NULL
|
|
-1-1
|
|
-2-2
|
|
11
|
|
drop table if exists t;
|
|
set @@tidb_enable_clustered_index = 1;
|
|
create table t (a int, b int, c int, primary key(a, b));
|
|
insert into t values (0, 0, 0);
|
|
insert into t values (1, 1, 1);
|
|
insert into t values (2, 2, 2);
|
|
insert into t values (3, 3, 3);
|
|
insert into t values (4, 4, 4);
|
|
insert into t values (5, 5, 5);
|
|
insert into t values (6, 6, 6);
|
|
insert into t values (7, 7, 7);
|
|
insert into t values (8, 8, 8);
|
|
insert into t values (9, 9, 9);
|
|
insert into t values (10, 10, 10);
|
|
select max(a), min(a+b) from t;
|
|
max(a) min(a+b)
|
|
10 0
|
|
select max(a+b), min(a+b) from t;
|
|
max(a+b) min(a+b)
|
|
20 0
|
|
select min(a), max(a), min(b), max(b) from t;
|
|
min(a) max(a) min(b) max(b)
|
|
0 10 0 10
|
|
set @@tidb_enable_clustered_index = default;
|
|
DROP TABLE IF EXISTS T;
|
|
CREATE TABLE T(A VARCHAR(10), B VARCHAR(10), C FLOAT);
|
|
INSERT INTO T VALUES('0', "val_b", 12.191);
|
|
SELECT MAX(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;
|
|
val_b
|
|
12.190999984741211
|
|
SELECT MIN(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;
|
|
val_b
|
|
12.190999984741211
|
|
drop table if exists t;
|
|
create table t (i int);
|
|
insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);
|
|
select i+1 as a, count(i+2), sum(i+3), group_concat(i+4), bit_or(i+5) from t group by i, hex(i+6) order by a;
|
|
a count(i+2) sum(i+3) group_concat(i+4) bit_or(i+5)
|
|
2 3 12 5,5,5 6
|
|
3 3 15 6,6,6 7
|
|
4 3 18 7,7,7 8
|
|
drop table if exists t;
|
|
create table t(a enum('a', 'b'));
|
|
insert into t values('a');
|
|
select a from t group by a;
|
|
a
|
|
a
|
|
drop table if exists t;
|
|
create table t(a datetime, b json, index idx(a));
|
|
insert into t values('2019-03-20 21:50:00', '["a", "b", 1]');
|
|
insert into t values('2019-03-20 21:50:01', '["a", "b", 1]');
|
|
insert into t values('2019-03-20 21:50:02', '["a", "b", 1]');
|
|
insert into t values('2019-03-20 21:50:03', '{"k1": "value", "k2": [10, 20]}');
|
|
insert into t values('2019-03-20 21:50:04', '{"k1": "value", "k2": [10, 20]}');
|
|
insert into t values('2019-03-20 21:50:05', '{"k1": "value", "k2": [10, 20]}');
|
|
insert into t values('2019-03-20 21:50:06', '"hello"');
|
|
insert into t values('2019-03-20 21:50:07', '"hello"');
|
|
insert into t values('2019-03-20 21:50:08', '"hello"');
|
|
set @@sql_mode='';
|
|
select b from t group by a order by a;
|
|
b
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
"hello"
|
|
"hello"
|
|
"hello"
|
|
select min(b) from t group by a order by a;
|
|
min(b)
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
"hello"
|
|
"hello"
|
|
"hello"
|
|
select max(b) from t group by a order by a;
|
|
max(b)
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
["a", "b", 1]
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
{"k1": "value", "k2": [10, 20]}
|
|
"hello"
|
|
"hello"
|
|
"hello"
|
|
set @@sql_mode=default;
|
|
drop table if exists t;
|
|
create table t(a char(10), b char(10));
|
|
insert into t values('1', '222'), ('12', '22');
|
|
select count(distinct a, b) from t;
|
|
count(distinct a, b)
|
|
2
|
|
select approx_count_distinct( a, b) from t;
|
|
approx_count_distinct( a, b)
|
|
2
|
|
drop table if exists t;
|
|
create table t(a char(10), b char(10));
|
|
insert into t values('1', '222'), ('12', '22');
|
|
select group_concat(distinct a, b) from t;
|
|
group_concat(distinct a, b)
|
|
1222,1222
|
|
drop table if exists t, s;
|
|
create table t(a int);
|
|
create table s(a int, b int);
|
|
insert into s values(100292, 508931), (120002, 508932);
|
|
insert into t values(508931), (508932);
|
|
select (select /*+ stream_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;
|
|
t
|
|
123-
|
|
123-
|
|
select (select /*+ hash_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;
|
|
t
|
|
123-
|
|
123-
|
|
CREATE TABLE `t49`(`c0` char(1) DEFAULT '1', `c2` char(1) DEFAULT NULL, UNIQUE KEY `c2` (`c2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
|
INSERT INTO `t49` VALUES ('0','0'),('0','1');
|
|
CREATE TABLE `t0` (`c0` blob DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
|
INSERT INTO `t0` VALUES (_binary ']'),(_binary '777926278'),(_binary '0.2136404982804636'),(_binary '1901362489'),(_binary '1558203848'),(''),(_binary '1830406335'),(''),(_binary '0'),(NULL),(_binary '601930250'),(_binary '1558203848'),(_binary '-122008948'),(_binary '-2053608489'),(_binary 'hb/vt <7'),(_binary 'RC&2*'),(_binary '1'),(_binary '-1722334316'),(_binary '1830406335'),(_binary '1372126029'),(_binary '882291196'),(NULL),(_binary '-399693596');
|
|
CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v0` (`c0`, `c1`, `c2`) AS SELECT NULL AS `NULL`,`t49`.`c2` AS `c2`,(((CASE _UTF8MB4'I되EkfIO퀶' WHEN NULL THEN `t49`.`c0` WHEN `t49`.`c2` THEN `t0`.`c0` ELSE (CASE `t49`.`c0` WHEN _UTF8MB4'%' THEN 1035293362 ELSE _UTF8MB4',' END) END))<<(`t49`.`c0`)) AS `(((CASE 'I되EkfIO퀶' WHEN NULL THEN t49.c0 WHEN t49.c2 THEN t0.c0 ELSE (CASE t49.c0 WHEN '%' THEN 1035293362 ELSE ',' END ) END ))<<(t49.c0))` FROM (`t0`) JOIN `t49` WHERE TRUE;
|
|
SELECT /*+ STREAM_AGG()*/v0.c0 FROM t49, v0 LEFT OUTER JOIN t0 ON ('Iw') GROUP BY true;
|
|
c0
|
|
NULL
|
|
drop table if exists t;
|
|
create table t(a json);
|
|
insert into t values ('{"id": 1,"score":23}');
|
|
insert into t values ('{"id": 2,"score":23}');
|
|
insert into t values ('{"id": 1,"score":233}');
|
|
insert into t values ('{"id": 2,"score":233}');
|
|
insert into t values ('{"id": 3,"score":233}');
|
|
set tidb_max_chunk_size = 2;
|
|
select max(JSON_EXTRACT(a, '$.score')) as max_score,JSON_EXTRACT(a,'$.id') as id from t group by id order by id;
|
|
max_score id
|
|
233 1
|
|
233 2
|
|
233 3
|
|
set tidb_max_chunk_size = default;
|
|
set tidb_max_chunk_size = 2;
|
|
drop table if exists t;
|
|
create table t(a int);
|
|
insert into t values(null),(null);
|
|
insert into t values(0),(2),(2),(4),(8);
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
0
|
|
2
|
|
4
|
|
8
|
|
drop table if exists t;
|
|
create table t(a float);
|
|
insert into t values(null),(null),(null),(null);
|
|
insert into t values(1.1),(1.1);
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
1.1
|
|
drop table if exists t;
|
|
create table t(a decimal(5,1));
|
|
insert into t values(null),(null),(null);
|
|
insert into t values(1.1),(2.2),(2.2);
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
1.1
|
|
2.2
|
|
drop table if exists t;
|
|
create table t(a datetime);
|
|
insert into t values(null);
|
|
insert into t values("2019-03-20 21:50:00"),("2019-03-20 21:50:01"), ("2019-03-20 21:50:00");
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
2019-03-20 21:50:00
|
|
2019-03-20 21:50:01
|
|
drop table if exists t;
|
|
create table t(a json);
|
|
insert into t values(null),(null),(null),(null);
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
drop table if exists t;
|
|
create table t(a char);
|
|
insert into t values(null),(null),(null),(null);
|
|
insert into t values('a'),('b');
|
|
select /*+ stream_agg() */ distinct * from t;
|
|
a
|
|
NULL
|
|
a
|
|
b
|
|
set tidb_max_chunk_size = default;
|
|
set tidb_max_chunk_size = 2;
|
|
drop table if exists t;
|
|
create table t(y year);
|
|
insert into t values (2020), (2000), (2050);
|
|
select sum(y) from t;
|
|
sum(y)
|
|
6070
|
|
select avg(y) from t;
|
|
avg(y)
|
|
2023.3333
|
|
set tidb_max_chunk_size = default;
|
|
drop table if exists t1;
|
|
CREATE TABLE t1 (
|
|
pk int(11) NOT NULL,
|
|
col1 decimal(40,20) DEFAULT NULL
|
|
);
|
|
INSERT INTO t1 VALUES (2084,0.02040000000000000000),(35324,0.02190000000000000000),(43760,0.00510000000000000000),(46084,0.01400000000000000000),(46312,0.00560000000000000000),(61632,0.02730000000000000000),(94676,0.00660000000000000000),(102244,0.01810000000000000000),(113144,0.02140000000000000000),(157024,0.02750000000000000000),(157144,0.01750000000000000000),(182076,0.02370000000000000000),(188696,0.02330000000000000000),(833,0.00390000000000000000),(6701,0.00230000000000000000),(8533,0.01690000000000000000),(13801,0.01360000000000000000),(20797,0.00680000000000000000),(36677,0.00550000000000000000),(46305,0.01290000000000000000),(76113,0.00430000000000000000),(76753,0.02400000000000000000),(92393,0.01720000000000000000),(111733,0.02690000000000000000),(152757,0.00250000000000000000),(162393,0.02760000000000000000),(167169,0.00440000000000000000),(168097,0.01360000000000000000),(180309,0.01720000000000000000),(19918,0.02620000000000000000),(58674,0.01820000000000000000),(67454,0.01510000000000000000),(70870,0.02880000000000000000),(89614,0.02530000000000000000),(106742,0.00180000000000000000),(107886,0.01580000000000000000),(147506,0.02230000000000000000),(148366,0.01340000000000000000),(167258,0.01860000000000000000),(194438,0.00500000000000000000),(10307,0.02850000000000000000),(14539,0.02210000000000000000),(27703,0.00050000000000000000),(32495,0.00680000000000000000),(39235,0.01450000000000000000),(52379,0.01640000000000000000),(54551,0.01910000000000000000),(85659,0.02330000000000000000),(104483,0.02670000000000000000),(109911,0.02040000000000000000),(114523,0.02110000000000000000),(119495,0.02120000000000000000),(137603,0.01910000000000000000),(154031,0.02580000000000000000);
|
|
SELECT count(distinct col1) FROM t1;
|
|
count(distinct col1)
|
|
48
|
|
drop table if exists t;
|
|
create table t(a tinyint(1));
|
|
insert into t values (-120), (127);
|
|
select avg(a) from t group by a;
|
|
avg(a)
|
|
-120.0000
|
|
127.0000
|
|
drop table t;
|
|
create table t(a smallint(1));
|
|
insert into t values (-120), (127);
|
|
select avg(a) from t group by a;
|
|
avg(a)
|
|
-120.0000
|
|
127.0000
|
|
drop table t;
|
|
create table t(a mediumint(1));
|
|
insert into t values (-120), (127);
|
|
select avg(a) from t group by a;
|
|
avg(a)
|
|
-120.0000
|
|
127.0000
|
|
drop table t;
|
|
create table t(a int(1));
|
|
insert into t values (-120), (127);
|
|
select avg(a) from t group by a;
|
|
avg(a)
|
|
-120.0000
|
|
127.0000
|
|
drop table t;
|
|
create table t(a bigint(1));
|
|
insert into t values (-120), (127);
|
|
select avg(a) from t group by a;
|
|
avg(a)
|
|
-120.0000
|
|
127.0000
|
|
drop table t;
|
|
drop table if exists td;
|
|
create table td (col_bigint bigint(20), col_smallint smallint(6));
|
|
insert into td values (null, 22876);
|
|
insert into td values (9220557287087669248, 32767);
|
|
insert into td values (28030, 32767);
|
|
insert into td values (-3309864251140603904,32767);
|
|
insert into td values (4,0);
|
|
insert into td values (null,0);
|
|
insert into td values (4,-23828);
|
|
insert into td values (54720,32767);
|
|
insert into td values (0,29815);
|
|
insert into td values (10017,-32661);
|
|
SELECT AVG( col_bigint / col_smallint) AS field1 FROM td;
|
|
field1
|
|
25769363061037.62077260
|
|
SELECT AVG(col_bigint) OVER (PARTITION BY col_smallint) as field2 FROM td where col_smallint = -23828;
|
|
field2
|
|
4.0000
|
|
drop table td;
|
|
drop table if exists t1;
|
|
create table t1(col1 time(2) NOT NULL);
|
|
insert into t1 values("16:40:20.01");
|
|
select col1 from t1 group by col1;
|
|
col1
|
|
16:40:20.01
|
|
drop table if exists t100;
|
|
set @@tidb_partition_prune_mode = 'static';
|
|
CREATE TABLE t100 (
|
|
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
col1 int(10) NOT NULL DEFAULT '0' COMMENT 'test',
|
|
money bigint(20) NOT NULL COMMENT 'test',
|
|
logtime datetime NOT NULL COMMENT '记录时间',
|
|
PRIMARY KEY (ID,logtime)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 COMMENT='test'
|
|
PARTITION BY RANGE COLUMNS(logtime) (
|
|
PARTITION p20220608 VALUES LESS THAN ("20220609"),
|
|
PARTITION p20220609 VALUES LESS THAN ("20220610"),
|
|
PARTITION p20220610 VALUES LESS THAN ("20220611"),
|
|
PARTITION p20220611 VALUES LESS THAN ("20220612"),
|
|
PARTITION p20220612 VALUES LESS THAN ("20220613"),
|
|
PARTITION p20220613 VALUES LESS THAN ("20220614"),
|
|
PARTITION p20220614 VALUES LESS THAN ("20220615"),
|
|
PARTITION p20220615 VALUES LESS THAN ("20220616"),
|
|
PARTITION p20220616 VALUES LESS THAN ("20220617"),
|
|
PARTITION p20220617 VALUES LESS THAN ("20220618"),
|
|
PARTITION p20220618 VALUES LESS THAN ("20220619"),
|
|
PARTITION p20220619 VALUES LESS THAN ("20220620"),
|
|
PARTITION p20220620 VALUES LESS THAN ("20220621"),
|
|
PARTITION p20220621 VALUES LESS THAN ("20220622"),
|
|
PARTITION p20220622 VALUES LESS THAN ("20220623"),
|
|
PARTITION p20220623 VALUES LESS THAN ("20220624"),
|
|
PARTITION p20220624 VALUES LESS THAN ("20220625")
|
|
);
|
|
insert into t100(col1,money,logtime) values (100,10,'2022-06-09 00:00:00');
|
|
insert into t100(col1,money,logtime) values (100,10,'2022-06-10 00:00:00');
|
|
SELECT /*+STREAM_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;
|
|
col1 sum(money)
|
|
100 20
|
|
SELECT /*+HASH_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;
|
|
col1 sum(money)
|
|
100 20
|
|
set @@tidb_partition_prune_mode = default;
|
|
drop table if exists t;
|
|
create table t(nname char(20));
|
|
insert into t values ('2'),(null),('11'),('2'),(null),('2'),(null),('11'),('33');
|
|
set @@group_concat_max_len=0;
|
|
select group_concat(nname order by 1 separator '#' ) from t;
|
|
group_concat(nname order by 1 separator '#' )
|
|
11#1
|
|
select group_concat(nname order by 1 desc separator '#' ) from t;
|
|
group_concat(nname order by 1 desc separator '#' )
|
|
33#2
|
|
set @@group_concat_max_len=default;
|
|
DROP TABLE IF EXISTS customer, orders;
|
|
CREATE TABLE `customer` ( `C_CUSTKEY` bigint(20) NOT NULL, `C_NAME` varchar(25) NOT NULL, `C_ADDRESS` varchar(40) NOT NULL, `C_NATIONKEY` bigint(20) NOT NULL, `C_PHONE` char(15) NOT NULL, `C_ACCTBAL` decimal(15,2) NOT NULL, `C_MKTSEGMENT` char(10) NOT NULL, `C_COMMENT` varchar(117) NOT NULL, PRIMARY KEY (`C_CUSTKEY`) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
|
CREATE TABLE `orders` ( `O_ORDERKEY` bigint(20) NOT NULL, `O_CUSTKEY` bigint(20) NOT NULL, `O_ORDERSTATUS` char(1) NOT NULL, `O_TOTALPRICE` decimal(15,2) NOT NULL, `O_ORDERDATE` date NOT NULL, `O_ORDERPRIORITY` char(15) NOT NULL, `O_CLERK` char(15) NOT NULL, `O_SHIPPRIORITY` bigint(20) NOT NULL, `O_COMMENT` varchar(79) NOT NULL, PRIMARY KEY (`O_ORDERKEY`) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
|
set tidb_opt_agg_push_down=ON;
|
|
explain format='plan_tree' SELECT /*+ hash_join_build(customer) */ c_custkey, count(o_orderkey) as c_count from customer left join orders on c_custkey = o_custkey and o_comment not like '%special%requests%' group by c_custkey;
|
|
id task access object operator info
|
|
Projection root executor__aggregate.customer.c_custkey, Column
|
|
└─HashAgg root group by:executor__aggregate.customer.c_custkey, funcs:count(Column)->Column, funcs:firstrow(executor__aggregate.customer.c_custkey)->executor__aggregate.customer.c_custkey
|
|
└─HashJoin root left outer join, left side:TableReader, equal:[eq(executor__aggregate.customer.c_custkey, executor__aggregate.orders.o_custkey)]
|
|
├─TableReader(Build) root data:TableFullScan
|
|
│ └─TableFullScan cop[tikv] table:customer keep order:false, stats:pseudo
|
|
└─HashAgg(Probe) root group by:executor__aggregate.orders.o_custkey, funcs:count(Column)->Column, funcs:firstrow(executor__aggregate.orders.o_custkey)->executor__aggregate.orders.o_custkey
|
|
└─TableReader root data:HashAgg
|
|
└─HashAgg cop[tikv] group by:executor__aggregate.orders.o_custkey, funcs:count(executor__aggregate.orders.o_orderkey)->Column
|
|
└─Selection cop[tikv] not(like(executor__aggregate.orders.o_comment, "%special%requests%", 92))
|
|
└─TableFullScan cop[tikv] table:orders keep order:false, stats:pseudo
|
|
set tidb_opt_agg_push_down=default;
|
|
SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
|
|
DROP TABLE IF EXISTS t1;
|
|
CREATE TABLE t1 (c1 ENUM('a', '', 'b'));
|
|
INSERT INTO t1 (c1) VALUES ('b');
|
|
INSERT INTO t1 (c1) VALUES ('');
|
|
INSERT INTO t1 (c1) VALUES ('a');
|
|
INSERT INTO t1 (c1) VALUES ('');
|
|
INSERT INTO t1 (c1) VALUES (0);
|
|
select * from t1;
|
|
c1
|
|
b
|
|
|
|
a
|
|
|
|
|
|
select c1 + 0 from t1;
|
|
c1 + 0
|
|
0
|
|
1
|
|
2
|
|
2
|
|
3
|
|
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
|
|
c1 + 0 COUNT(c1)
|
|
0 1
|
|
1 1
|
|
2 2
|
|
3 1
|
|
alter table t1 add index idx(c1);
|
|
select c1 + 0 from t1;
|
|
c1 + 0
|
|
0
|
|
1
|
|
2
|
|
2
|
|
3
|
|
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
|
|
c1 + 0 COUNT(c1)
|
|
0 1
|
|
1 1
|
|
2 2
|
|
3 1
|
|
DROP TABLE IF EXISTS t1;
|
|
CREATE TABLE t1 (c1 ENUM('a', 'b', 'c'));
|
|
INSERT INTO t1 (c1) VALUES ('b');
|
|
INSERT INTO t1 (c1) VALUES ('a');
|
|
INSERT INTO t1 (c1) VALUES ('b');
|
|
INSERT INTO t1 (c1) VALUES ('c');
|
|
INSERT INTO t1 (c1) VALUES (0);
|
|
select * from t1;
|
|
c1
|
|
b
|
|
a
|
|
b
|
|
c
|
|
|
|
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
|
|
c1 + 0 COUNT(c1)
|
|
0 1
|
|
1 1
|
|
2 2
|
|
3 1
|
|
SET sql_mode = default;
|
|
set @@tidb_hash_join_concurrency=1;
|
|
set sql_mode='STRICT_TRANS_TABLES';
|
|
drop table if exists t;
|
|
create table t (c int, d int);
|
|
insert t values (NULL, 1);
|
|
insert t values (1, 1);
|
|
insert t values (1, 2);
|
|
insert t values (1, 3);
|
|
insert t values (1, 1);
|
|
insert t values (3, 2);
|
|
insert t values (4, 3);
|
|
select bit_and(c) from t where NULL;
|
|
bit_and(c)
|
|
18446744073709551615
|
|
select bit_or(c) from t where NULL;
|
|
bit_or(c)
|
|
0
|
|
select bit_xor(c) from t where NULL;
|
|
bit_xor(c)
|
|
0
|
|
select approx_count_distinct(c) from t where NULL;
|
|
approx_count_distinct(c)
|
|
0
|
|
select count(*) from t;
|
|
count(*)
|
|
7
|
|
select count(*) from t group by d order by c;
|
|
count(*)
|
|
3
|
|
2
|
|
2
|
|
select distinct 99 from t group by d having d > 0;
|
|
99
|
|
99
|
|
select count(*) from t having 1 = 0;
|
|
count(*)
|
|
select c,d from t group by d order by d;
|
|
c d
|
|
NULL 1
|
|
1 2
|
|
1 3
|
|
select - c, c as d from t group by c having null not between c and avg(distinct d) - d;
|
|
- c d
|
|
select - c as c from t group by c having t.c > 5;
|
|
c
|
|
select t1.c from t t1, t t2 group by c having c > 5;
|
|
c
|
|
select count(*) from (select d, c from t) k where d != 0 group by d order by c;
|
|
count(*)
|
|
3
|
|
2
|
|
2
|
|
select c as a from t group by d having a < 0;
|
|
a
|
|
select c as a from t group by d having sum(a) = 2;
|
|
a
|
|
NULL
|
|
select count(distinct c) from t group by d order by c;
|
|
count(distinct c)
|
|
1
|
|
2
|
|
2
|
|
select approx_count_distinct(c) from t group by d order by c;
|
|
approx_count_distinct(c)
|
|
1
|
|
2
|
|
2
|
|
select sum(c) as a from t group by d order by a;
|
|
a
|
|
2
|
|
4
|
|
5
|
|
select sum(c) as a, sum(c+1), sum(c), sum(c+1) from t group by d order by a;
|
|
a sum(c+1) sum(c) sum(c+1)
|
|
2 4 2 4
|
|
4 6 4 6
|
|
5 7 5 7
|
|
select count(distinct c,d) from t;
|
|
count(distinct c,d)
|
|
5
|
|
select approx_count_distinct(c,d) from t;
|
|
approx_count_distinct(c,d)
|
|
5
|
|
select count(c,d) from t;
|
|
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 15 near ",d) from t"
|
|
select d*2 as ee, sum(c) from t group by ee order by ee;
|
|
ee sum(c)
|
|
2 2
|
|
4 4
|
|
6 5
|
|
select sum(distinct c) as a from t group by d order by a;
|
|
a
|
|
1
|
|
4
|
|
5
|
|
select min(c) as a from t group by d order by a;
|
|
a
|
|
1
|
|
1
|
|
1
|
|
select max(c) as a from t group by d order by a;
|
|
a
|
|
1
|
|
3
|
|
4
|
|
select avg(c) as a from t group by d order by a;
|
|
a
|
|
1.0000
|
|
2.0000
|
|
2.5000
|
|
select c, approx_count_distinct(d) as a from t group by c order by a, c;
|
|
c a
|
|
NULL 1
|
|
3 1
|
|
4 1
|
|
1 3
|
|
select d, d + 1 from t group by d order by d;
|
|
d d + 1
|
|
1 2
|
|
2 3
|
|
3 4
|
|
select count(*) from t;
|
|
count(*)
|
|
7
|
|
select count(distinct d) from t;
|
|
count(distinct d)
|
|
3
|
|
select approx_count_distinct(d) from t;
|
|
approx_count_distinct(d)
|
|
3
|
|
select count(*) as a from t group by d having sum(c) > 3 order by a;
|
|
a
|
|
2
|
|
2
|
|
select max(c) from t group by d having sum(c) > 3 order by avg(c) desc;
|
|
max(c)
|
|
4
|
|
3
|
|
select sum(-1) from t a left outer join t b on not null is null;
|
|
sum(-1)
|
|
-7
|
|
select count(*), b.d from t a left join t b on a.c = b.d group by b.d order by b.d;
|
|
count(*) d
|
|
2 NULL
|
|
12 1
|
|
2 3
|
|
select count(b.d), b.d from t a left join t b on a.c = b.d group by b.d order by b.d;
|
|
count(b.d) d
|
|
0 NULL
|
|
12 1
|
|
2 3
|
|
select count(b.d), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
|
|
count(b.d) d
|
|
0 NULL
|
|
12 1
|
|
2 3
|
|
select count(*), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
|
|
count(*) d
|
|
2 NULL
|
|
12 1
|
|
2 3
|
|
select max(case when b.d is null then 10 else b.c end), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
|
|
max(case when b.d is null then 10 else b.c end) d
|
|
10 NULL
|
|
1 1
|
|
4 3
|
|
select count(*) from t a , t b;
|
|
count(*)
|
|
49
|
|
select count(*) from t a , t b, t c;
|
|
count(*)
|
|
343
|
|
select count(*) from t a , t b where a.c = b.d;
|
|
count(*)
|
|
14
|
|
select count(a.d), sum(b.c) from t a , t b where a.c = b.d order by a.d;
|
|
count(a.d) sum(b.c)
|
|
14 13
|
|
select count(*) from t a , t b, t c where a.c = b.d and b.d = c.d;
|
|
count(*)
|
|
40
|
|
select count(*), a.c from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by a.c;
|
|
count(*) c
|
|
36 1
|
|
4 3
|
|
select count(a.c), c.d from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by c.d;
|
|
count(a.c) d
|
|
36 1
|
|
4 3
|
|
select count(*) from t a , t b where a.c = b.d and a.c + b.d = 2;
|
|
count(*)
|
|
12
|
|
select count(*) from t a join t b having sum(a.c) < 0;
|
|
count(*)
|
|
select count(*) from t a join t b where a.c < 0;
|
|
count(*)
|
|
0
|
|
select sum(b.c), count(b.d), a.c from t a left join t b on a.c = b.d group by b.d order by b.d;
|
|
sum(b.c) count(b.d) c
|
|
NULL 0 NULL
|
|
8 12 1
|
|
5 2 3
|
|
select 1-d as d from t having d < 0 order by d desc;
|
|
d
|
|
-1
|
|
-1
|
|
-2
|
|
-2
|
|
select 1-d as d from t having d + 1 < 0 order by d + 1;
|
|
d
|
|
-2
|
|
-2
|
|
drop table if exists t;
|
|
create table t (keywords varchar(20), type int);
|
|
insert into t values('测试', 1), ('test', 2);
|
|
select group_concat(keywords) from t group by type order by type;
|
|
group_concat(keywords)
|
|
测试
|
|
test
|
|
drop table if exists t;
|
|
create table t (c int, d int);
|
|
insert t values (1, -1);
|
|
insert t values (1, 0);
|
|
insert t values (1, 1);
|
|
select d, d*d as d from t having d = -1;
|
|
d d
|
|
select d*d as d from t group by d having d = -1;
|
|
d
|
|
1
|
|
select d, 1-d as d, c as d from t order by d;
|
|
d d d
|
|
1 0 1
|
|
0 1 1
|
|
-1 2 1
|
|
select d, 1-d as d, c as d from t order by d+1;
|
|
d d d
|
|
-1 2 1
|
|
0 1 1
|
|
1 0 1
|
|
select d, 1-d as d, c as d from t group by d order by d;
|
|
d d d
|
|
1 0 1
|
|
0 1 1
|
|
-1 2 1
|
|
select d as d1, t.d as d1, 1-d as d1, c as d1 from t having d1 < 10 order by d;
|
|
d1 d1 d1 d1
|
|
-1 -1 2 1
|
|
0 0 1 1
|
|
1 1 0 1
|
|
select d*d as d1, c as d1 from t group by d1 order by d1;
|
|
d1 d1
|
|
0 1
|
|
1 1
|
|
select d*d as d1, c as d1 from t group by 2;
|
|
d1 d1
|
|
1 1
|
|
select * from t group by 2 order by d;
|
|
c d
|
|
1 -1
|
|
1 0
|
|
1 1
|
|
select * , sum(d) from t group by 1 order by d;
|
|
c d sum(d)
|
|
1 -1 0
|
|
select sum(d), t.* from t group by 2 order by d;
|
|
sum(d) c d
|
|
0 1 -1
|
|
select d as d, c as d from t group by d + 1 order by t.d;
|
|
d d
|
|
-1 1
|
|
0 1
|
|
1 1
|
|
select c as d, c as d from t group by d order by d;
|
|
d d
|
|
1 1
|
|
1 1
|
|
1 1
|
|
select d as d, c as d from t group by d;
|
|
Error 1052 (23000): Column 'd' in group statement is ambiguous
|
|
select t.d, c as d from t group by d;
|
|
Error 1052 (23000): Column 'd' in group statement is ambiguous
|
|
select *, c+1 as d from t group by 3 order by d;
|
|
c d d
|
|
1 -1 2
|
|
drop table if exists t1;
|
|
create table t1(a float, b int default 3);
|
|
insert into t1 (a) values (2), (11), (8);
|
|
select min(a), min(case when 1=1 then a else NULL end), min(case when 1!=1 then NULL else a end) from t1 where b=3 group by b;
|
|
min(a) min(case when 1=1 then a else NULL end) min(case when 1!=1 then NULL else a end)
|
|
2 2 2
|
|
drop table if exists t1;
|
|
create table t1(a int, index(a));
|
|
insert into t1 (a) values (1),(2),(3),(4),(5);
|
|
select count(a) from t1 where a < 3;
|
|
count(a)
|
|
2
|
|
drop table if exists t1;
|
|
create table t1(a int, b int, index(a));
|
|
select sum(b) from (select * from t1) t group by a;
|
|
sum(b)
|
|
select sum(b) from (select * from t1) t;
|
|
sum(b)
|
|
NULL
|
|
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4),(3, 5);
|
|
select avg(b) from (select * from t1) t group by a order by a;
|
|
avg(b)
|
|
2.5000
|
|
2.0000
|
|
4.0000
|
|
select sum(b) from (select * from t1) t group by a order by a;
|
|
sum(b)
|
|
5
|
|
2
|
|
8
|
|
select count(b) from (select * from t1) t group by a order by a;
|
|
count(b)
|
|
2
|
|
1
|
|
2
|
|
select max(b) from (select * from t1) t group by a order by a;
|
|
max(b)
|
|
4
|
|
2
|
|
5
|
|
select min(b) from (select * from t1) t group by a order by a;
|
|
min(b)
|
|
1
|
|
2
|
|
3
|
|
drop table if exists t1;
|
|
create table t1(a int, b int, index(a,b));
|
|
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3);
|
|
select avg(distinct b) from (select * from t1) t group by a order by a;
|
|
avg(distinct b)
|
|
2.5000
|
|
2.0000
|
|
4.0000
|
|
select sum(distinct b) from (select * from t1) t group by a order by a;
|
|
sum(distinct b)
|
|
5
|
|
2
|
|
8
|
|
select count(distinct b) from (select * from t1) t group by a order by a;
|
|
count(distinct b)
|
|
2
|
|
1
|
|
2
|
|
select approx_count_distinct(b) from (select * from t1) t group by a order by a;
|
|
approx_count_distinct(b)
|
|
2
|
|
1
|
|
2
|
|
select max(distinct b) from (select * from t1) t group by a order by a;
|
|
max(distinct b)
|
|
4
|
|
2
|
|
5
|
|
select min(distinct b) from (select * from t1) t group by a order by a;
|
|
min(distinct b)
|
|
1
|
|
2
|
|
3
|
|
drop table if exists t1;
|
|
create table t1(a int, b int, index(b, a));
|
|
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3);
|
|
select avg(distinct b) from (select * from t1) t group by a order by a;
|
|
avg(distinct b)
|
|
2.5000
|
|
2.0000
|
|
4.0000
|
|
select sum(distinct b) from (select * from t1) t group by a order by a;
|
|
sum(distinct b)
|
|
5
|
|
2
|
|
8
|
|
select count(distinct b) from (select * from t1) t group by a order by a;
|
|
count(distinct b)
|
|
2
|
|
1
|
|
2
|
|
select max(distinct b) from (select * from t1) t group by a order by a;
|
|
max(distinct b)
|
|
4
|
|
2
|
|
5
|
|
select min(distinct b) from (select * from t1) t group by a order by a;
|
|
min(distinct b)
|
|
1
|
|
2
|
|
3
|
|
drop table if exists t;
|
|
create table t (id int primary key, ds date);
|
|
insert into t (id, ds) values (1, "1991-09-05"),(2,"1991-09-05"), (3, "1991-09-06"),(0,"1991-09-06");
|
|
select sum(id), ds from t group by ds order by id;
|
|
sum(id) ds
|
|
3 1991-09-06
|
|
3 1991-09-05
|
|
drop table if exists t1;
|
|
drop table if exists t2;
|
|
create table t1 (col0 int, col1 int);
|
|
create table t2 (col0 int, col1 int);
|
|
insert into t1 values(83, 0), (26, 0), (43, 81);
|
|
insert into t2 values(22, 2), (3, 12), (38, 98);
|
|
SELECT COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL ) FROM t2, t1 AS cor0, t2 AS cor1 GROUP BY cor0.col1;
|
|
COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL )
|
|
NULL
|
|
NULL
|
|
drop table if exists t1;
|
|
drop table if exists t2;
|
|
create table t1 (c1 int);
|
|
create table t2 (c1 int);
|
|
insert into t1 values(3), (2);
|
|
insert into t2 values(1), (2);
|
|
set @@session.tidb_opt_insubq_to_join_and_agg = 0;
|
|
select sum(c1 in (select * from t2)) from t1;
|
|
sum(c1 in (select * from t2))
|
|
1
|
|
set @@session.tidb_opt_insubq_to_join_and_agg = 1;
|
|
select sum(c1 in (select * from t2)) from t1;
|
|
sum(c1 in (select * from t2))
|
|
1
|
|
select sum(c1) k from (select * from t1 union all select * from t2)t group by c1 * 2 order by k;
|
|
k
|
|
1
|
|
3
|
|
4
|
|
drop table if exists t;
|
|
create table t (a int, b int, c int);
|
|
insert into t values(1, 2, 3), (1, 2, 4);
|
|
select count(distinct c), count(distinct a,b) from t;
|
|
count(distinct c) count(distinct a,b)
|
|
2 1
|
|
select approx_count_distinct( c), approx_count_distinct( a,b) from t;
|
|
approx_count_distinct( c) approx_count_distinct( a,b)
|
|
2 1
|
|
drop table if exists t;
|
|
create table t (a float);
|
|
insert into t values(966.36), (363.97), (569.99), (453.33), (376.45), (321.93), (12.12), (45.77), (9.66), (612.17);
|
|
select distinct count(distinct a) from t;
|
|
count(distinct a)
|
|
10
|
|
select distinct approx_count_distinct( a) from t;
|
|
approx_count_distinct( a)
|
|
10
|
|
create table idx_agg (a int, b int, index (b));
|
|
insert idx_agg values (1, 1), (1, 2), (2, 2);
|
|
select sum(a), sum(b) from idx_agg where b > 0 and b < 10;
|
|
sum(a) sum(b)
|
|
4 5
|
|
select 10 from idx_agg group by b;
|
|
10
|
|
10
|
|
10
|
|
select 11 from idx_agg group by a;
|
|
11
|
|
11
|
|
11
|
|
set @@tidb_init_chunk_size=1;
|
|
select group_concat(b) from idx_agg group by b;
|
|
group_concat(b)
|
|
1
|
|
2,2
|
|
set @@tidb_init_chunk_size=2;
|
|
drop table if exists t;
|
|
create table t(a int(11), b decimal(15,2));
|
|
insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97);
|
|
select a, max(b) from t group by a order by a limit 2;
|
|
a max(b)
|
|
1 771.64
|
|
2 378.49
|
|
drop table if exists t;
|
|
create table t(a int(11), b char(15));
|
|
insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97);
|
|
select a, max(b) from t group by a order by a limit 2;
|
|
a max(b)
|
|
1 771.64
|
|
2 378.49
|
|
drop table if exists t;
|
|
create table t (id int(11) NOT NULL, tags json DEFAULT NULL);
|
|
insert into t values (1, '{"i": 1, "n": "n1"}');
|
|
insert into t values (2, '{"i": 2, "n": "n2"}');
|
|
insert into t values (3, '{"i": 3, "n": "n3"}');
|
|
insert into t values (4, '{"i": 4, "n": "n4"}');
|
|
insert into t values (5, '{"i": 5, "n": "n5"}');
|
|
insert into t values (6, '{"i": 0, "n": "n6"}');
|
|
insert into t values (7, '{"i": -1, "n": "n7"}');
|
|
select sum(tags->'$.i') from t;
|
|
sum(tags->'$.i')
|
|
14
|
|
select id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95) from t where null;
|
|
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95)
|
|
NULL 0 NULL NULL 0 18446744073709551615 0 NULL NULL NULL
|
|
truncate table t;
|
|
drop table if exists s;
|
|
create table s(id int);
|
|
select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id;
|
|
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95) approx_count_distinct(95)
|
|
NULL 0 NULL NULL 0 18446744073709551615 0 NULL NULL NULL 0
|
|
insert into t values (1, '{"i": 1, "n": "n1"}');
|
|
select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id;
|
|
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95) approx_count_distinct(95)
|
|
1 1 95 95.0000 95 95 95 95 95 95 1
|
|
set @@tidb_hash_join_concurrency=5;
|
|
drop table t;
|
|
CREATE TABLE `t` (`a` bit(1) NOT NULL, PRIMARY KEY (`a`));
|
|
insert into t value(1), (0);
|
|
select a from t group by 1;
|
|
a
|
|
|