olap.result 17.3 KB
Newer Older
1
drop table if exists t1,t2;
2 3 4 5 6
set @sav_dpi= @@div_precision_increment;
set div_precision_increment= 5;
show variables like 'div_precision_increment';
Variable_name	Value
div_precision_increment	5
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
create table t1 (product varchar(32), country_id int not null, year int, profit int);
insert into t1  values ( 'Computer', 2,2000, 1200),
( 'TV', 1, 1999, 150),
( 'Calculator', 1, 1999,50),
( 'Computer', 1, 1999,1500),
( 'Computer', 1, 2000,1500),
( 'TV', 1, 2000, 150),
( 'TV', 2, 2000, 100),
( 'TV', 2, 2000, 100),
( 'Calculator', 1, 2000,75),
( 'Calculator', 2, 2000,75),
( 'TV', 1, 1999, 100),
( 'Computer', 1, 1999,1200),
( 'Computer', 2, 2000,1500),
( 'Calculator', 2, 2000,75),
( 'Phone', 3, 2003,10)
;
create table t2 (country_id int primary key, country char(20) not null);
insert into t2 values (1, 'USA'),(2,'India'), (3,'Finland');
select product, sum(profit) from t1 group by product;
product	sum(profit)
Calculator	275
Computer	6900
Phone	10
TV	600
select product, sum(profit) from t1 group by product with rollup;
product	sum(profit)
Calculator	275
Computer	6900
Phone	10
TV	600
NULL	7785
select product, sum(profit) from t1 group by 1 with rollup;
product	sum(profit)
Calculator	275
Computer	6900
Phone	10
TV	600
NULL	7785
select product, sum(profit),avg(profit) from t1 group by product with rollup;
product	sum(profit)	avg(profit)
48 49 50 51 52
Calculator	275	68.75000
Computer	6900	1380.00000
Phone	10	10.00000
TV	600	120.00000
NULL	7785	519.00000
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 88
select product, country_id , year, sum(profit) from t1 group by product, country_id, year;
product	country_id	year	sum(profit)
Calculator	1	1999	50
Calculator	1	2000	75
Calculator	2	2000	150
Computer	1	1999	2700
Computer	1	2000	1500
Computer	2	2000	2700
Phone	3	2003	10
TV	1	1999	250
TV	1	2000	150
TV	2	2000	200
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup;
product	country_id	year	sum(profit)
Calculator	1	1999	50
Calculator	1	2000	75
Calculator	1	NULL	125
Calculator	2	2000	150
Calculator	2	NULL	150
Calculator	NULL	NULL	275
Computer	1	1999	2700
Computer	1	2000	1500
Computer	1	NULL	4200
Computer	2	2000	2700
Computer	2	NULL	2700
Computer	NULL	NULL	6900
Phone	3	2003	10
Phone	3	NULL	10
Phone	NULL	NULL	10
TV	1	1999	250
TV	1	2000	150
TV	1	NULL	400
TV	2	2000	200
TV	2	NULL	200
TV	NULL	NULL	600
NULL	NULL	NULL	7785
89
explain extended select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup;
90
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
91
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	15	Using filesort
92
Warnings:
93
Note	1003	select `test`.`t1`.`product` AS `product`,`test`.`t1`.`country_id` AS `country_id`,`test`.`t1`.`year` AS `year`,sum(`test`.`t1`.`profit`) AS `sum(profit)` from `test`.`t1` group by `test`.`t1`.`product`,`test`.`t1`.`country_id`,`test`.`t1`.`year` with rollup
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
select product, country_id , sum(profit) from t1 group by product desc, country_id with rollup;
product	country_id	sum(profit)
TV	1	400
TV	2	200
TV	NULL	600
Phone	3	10
Phone	NULL	10
Computer	1	4200
Computer	2	2700
Computer	NULL	6900
Calculator	1	125
Calculator	2	150
Calculator	NULL	275
NULL	NULL	7785
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup limit 5;
product	country_id	year	sum(profit)
Calculator	1	1999	50
Calculator	1	2000	75
Calculator	1	NULL	125
Calculator	2	2000	150
Calculator	2	NULL	150
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup limit 3,3;
product	country_id	year	sum(profit)
Calculator	2	2000	150
Calculator	2	NULL	150
Calculator	NULL	NULL	275
select product, country_id, count(*), count(distinct year) from t1 group by product, country_id;
product	country_id	count(*)	count(distinct year)
Calculator	1	2	2
Calculator	2	2	1
Computer	1	3	2
Computer	2	2	1
Phone	3	1	1
TV	1	3	2
TV	2	2	1
select product, country_id, count(*), count(distinct year) from t1 group by product, country_id with rollup;
product	country_id	count(*)	count(distinct year)
Calculator	1	2	2
Calculator	2	2	1
Calculator	NULL	4	2
Computer	1	3	2
Computer	2	2	1
Computer	NULL	5	2
Phone	3	1	1
Phone	NULL	1	1
TV	1	3	2
TV	2	2	1
TV	NULL	5	2
NULL	NULL	15	3
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup having country_id = 1;
product	country_id	year	sum(profit)
Calculator	1	1999	50
Calculator	1	2000	75
Calculator	1	NULL	125
Computer	1	1999	2700
Computer	1	2000	1500
Computer	1	NULL	4200
TV	1	1999	250
TV	1	2000	150
TV	1	NULL	400
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup having sum(profit) > 200;
product	country_id	year	sum(profit)
Calculator	NULL	NULL	275
Computer	1	1999	2700
Computer	1	2000	1500
Computer	1	NULL	4200
Computer	2	2000	2700
Computer	2	NULL	2700
Computer	NULL	NULL	6900
TV	1	1999	250
TV	1	NULL	400
TV	NULL	NULL	600
NULL	NULL	NULL	7785
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup having sum(profit) > 7000;
product	country_id	year	sum(profit)
NULL	NULL	NULL	7785
select concat(product,':',country_id) as 'prod', concat(":",year,":") as 'year',1+1, sum(profit)/count(*) from t1 group by 1,2 with rollup;
prod	year	1+1	sum(profit)/count(*)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
Calculator:1	:1999:	2	50.00000
Calculator:1	:2000:	2	75.00000
Calculator:1	NULL	2	62.50000
Calculator:2	:2000:	2	75.00000
Calculator:2	NULL	2	75.00000
Computer:1	:1999:	2	1350.00000
Computer:1	:2000:	2	1500.00000
Computer:1	NULL	2	1400.00000
Computer:2	:2000:	2	1350.00000
Computer:2	NULL	2	1350.00000
Phone:3	:2003:	2	10.00000
Phone:3	NULL	2	10.00000
TV:1	:1999:	2	125.00000
TV:1	:2000:	2	150.00000
TV:1	NULL	2	133.33333
TV:2	:2000:	2	100.00000
TV:2	NULL	2	100.00000
NULL	NULL	2	519.00000
190 191
select product, sum(profit)/count(*) from t1 group by product with rollup;
product	sum(profit)/count(*)
192 193 194 195 196
Calculator	68.75000
Computer	1380.00000
Phone	10.00000
TV	120.00000
NULL	519.00000
197 198
select left(product,4) as prod, sum(profit)/count(*) from t1 group by prod with rollup;
prod	sum(profit)/count(*)
199 200 201 202 203
Calc	68.75000
Comp	1380.00000
Phon	10.00000
TV	120.00000
NULL	519.00000
204 205
select concat(product,':',country_id), 1+1, sum(profit)/count(*) from t1 group by concat(product,':',country_id) with rollup;
concat(product,':',country_id)	1+1	sum(profit)/count(*)
206 207 208 209 210 211 212 213
Calculator:1	2	62.50000
Calculator:2	2	75.00000
Computer:1	2	1400.00000
Computer:2	2	1350.00000
Phone:3	2	10.00000
TV:1	2	133.33333
TV:2	2	100.00000
NULL	2	519.00000
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
select product, country , year, sum(profit) from t1,t2 where t1.country_id=t2.country_id group by product, country, year with rollup;
product	country	year	sum(profit)
Calculator	India	2000	150
Calculator	India	NULL	150
Calculator	USA	1999	50
Calculator	USA	2000	75
Calculator	USA	NULL	125
Calculator	NULL	NULL	275
Computer	India	2000	2700
Computer	India	NULL	2700
Computer	USA	1999	2700
Computer	USA	2000	1500
Computer	USA	NULL	4200
Computer	NULL	NULL	6900
Phone	Finland	2003	10
Phone	Finland	NULL	10
Phone	NULL	NULL	10
TV	India	2000	200
TV	India	NULL	200
TV	USA	1999	250
TV	USA	2000	150
TV	USA	NULL	400
TV	NULL	NULL	600
NULL	NULL	NULL	7785
select product, `sum` from (select product, sum(profit) as 'sum' from t1 group by product with rollup) as tmp where product is null;
product	sum
NULL	7785
select product from t1 where exists (select product, country_id , sum(profit) from t1 as t2 where t1.product=t2.product group by product, country_id with rollup having sum(profit) > 6000);
product
Computer
Computer
Computer
Computer
Computer
select product, country_id , year, sum(profit) from t1 group by product, country_id, year having country_id is NULL;
product	country_id	year	sum(profit)
select concat(':',product,':'), sum(profit),avg(profit) from t1 group by product with rollup;
concat(':',product,':')	sum(profit)	avg(profit)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
252 253 254 255 256
:Calculator:	275	68.75000
:Computer:	6900	1380.00000
:Phone:	10	10.00000
:TV:	600	120.00000
NULL	7785	519.00000
257 258 259 260 261 262 263
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with cube;
ERROR 42000: This version of MySQL doesn't yet support 'CUBE'
explain select product, country_id , year, sum(profit) from t1 group by product, country_id, year with cube;
ERROR 42000: This version of MySQL doesn't yet support 'CUBE'
select product, country_id , year, sum(profit) from t1 group by product, country_id, year with cube union all select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup;
ERROR 42000: This version of MySQL doesn't yet support 'CUBE'
drop table t1,t2;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES(100);
CREATE TABLE t2 (i int);
INSERT INTO t2 VALUES (100),(200);
SELECT i, COUNT(*) FROM t1 GROUP BY i WITH ROLLUP;
i	COUNT(*)
100	1
NULL	1
SELECT t1.i, t2.i, COUNT(*) FROM t1,t2 GROUP BY t1.i,t2.i WITH ROLLUP;
i	i	COUNT(*)
100	100	1
100	200	1
100	NULL	2
NULL	NULL	2
drop table t1,t2;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
CREATE TABLE user_day(
user_id INT NOT NULL,
date DATE NOT NULL,
UNIQUE INDEX user_date (user_id, date)
);
INSERT INTO user_day VALUES
(1, '2004-06-06' ),
(1, '2004-06-07' ),
(2, '2004-06-06' );
SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day 
ON next_day.user_id=d.user_id AND 
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day;
day	sample	not_cancelled
2004-06-06	2	1
2004-06-07	1	0
SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day 
ON next_day.user_id=d.user_id AND 
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day
WITH ROLLUP;
day	sample	not_cancelled
2004-06-06	2	1
2004-06-07	1	0
NULL	3	1
DROP TABLE user_day;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES
(1,4),
(2,2), (2,2),
(4,1), (4,1), (4,1), (4,1),
(2,1), (2,1);
SELECT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)
4
6
4
14
SELECT DISTINCT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)
4
6
14
SELECT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(DISTINCT b)
4	1
6	2
4	1
14	3
SELECT DISTINCT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(DISTINCT b)
4	1
6	2
14	3
SELECT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(*)
4	1
6	4
4	4
14	9
SELECT DISTINCT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(*)
4	1
6	4
4	4
14	9
SELECT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(DISTINCT b)	COUNT(*)
4	1	1
6	2	4
4	1	4
14	3	9
SELECT DISTINCT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1
GROUP BY a WITH ROLLUP;
SUM(b)	COUNT(DISTINCT b)	COUNT(*)
4	1	1
6	2	4
4	1	4
14	3	9
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
SELECT a, sum(b) FROM t1 GROUP BY a,b WITH ROLLUP;
a	sum(b)
1	4
1	4
2	2
2	4
2	6
4	4
4	4
NULL	14
SELECT DISTINCT a, sum(b) FROM t1 GROUP BY a,b WITH ROLLUP;
a	sum(b)
1	4
2	2
2	4
2	6
4	4
NULL	14
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
SELECT b, a, sum(b) FROM t1 GROUP BY a,b WITH ROLLUP;
b	a	sum(b)
4	1	4
NULL	1	4
1	2	2
2	2	4
NULL	2	6
1	4	4
NULL	4	4
NULL	NULL	14
SELECT DISTINCT b,a, sum(b) FROM t1 GROUP BY a,b WITH ROLLUP;
b	a	sum(b)
4	1	4
NULL	1	4
1	2	2
2	2	4
NULL	2	6
1	4	4
NULL	4	4
NULL	NULL	14
ALTER TABLE t1 ADD COLUMN c INT;
SELECT a,b,sum(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;
a	b	sum(c)
1	4	NULL
1	4	NULL
1	NULL	NULL
2	1	NULL
2	1	NULL
2	2	NULL
2	2	NULL
2	NULL	NULL
4	1	NULL
4	1	NULL
4	NULL	NULL
NULL	NULL	NULL
SELECT distinct a,b,sum(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;
a	b	sum(c)
1	4	NULL
1	NULL	NULL
2	1	NULL
2	2	NULL
2	NULL	NULL
4	1	NULL
4	NULL	NULL
NULL	NULL	NULL
431
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
432 433 434 435 436 437 438 439 440 441 442 443 444
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES
(1,4),
(2,2), (2,2),
(4,1), (4,1), (4,1), (4,1),
(2,1), (2,1);
SELECT a, SUM(b) FROM t1 GROUP BY a WITH ROLLUP LIMIT 1;
a	SUM(b)
1	4
SELECT SQL_CALC_FOUND_ROWS a, SUM(b) FROM t1 GROUP BY a WITH ROLLUP LIMIT 1;
a	SUM(b)
1	4
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
445 446 447 448 449 450 451 452 453 454 455 456 457
CREATE TABLE t1 (a int(11) NOT NULL);
INSERT INTO t1 VALUES (1),(2);
SELECT a, SUM(a) m FROM  t1 GROUP BY a WITH ROLLUP;
a	m
1	1
2	2
NULL	3
SELECT * FROM ( SELECT a, SUM(a) m FROM  t1 GROUP BY a WITH ROLLUP ) t2;
a	m
1	1
2	2
NULL	3
DROP TABLE t1;
458
set div_precision_increment= @sav_dpi;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
CREATE TABLE t1 (a int(11));
INSERT INTO t1 VALUES (1),(2);
SELECT a, SUM(a), SUM(a)+1 FROM (SELECT a FROM t1 UNION select 2) d 
GROUP BY a;
a	SUM(a)	SUM(a)+1
1	1	2
2	2	3
SELECT a, SUM(a), SUM(a)+1 FROM (SELECT a FROM t1 UNION select 2) d 
GROUP BY a WITH ROLLUP;
a	SUM(a)	SUM(a)+1
1	1	2
2	2	3
NULL	3	4
SELECT a, SUM(a), SUM(a)+1 FROM (SELECT 1 a UNION select 2) d 
GROUP BY a;
a	SUM(a)	SUM(a)+1
1	1	2
2	2	3
SELECT a, SUM(a), SUM(a)+1 FROM (SELECT 1 a UNION select 2) d 
GROUP BY a WITH ROLLUP;
a	SUM(a)	SUM(a)+1
1	1	2
2	2	3
NULL	3	4
SELECT a, SUM(a), SUM(a)+1, CONCAT(SUM(a),'x'), SUM(a)+SUM(a), SUM(a)
FROM (SELECT 1 a, 2 b UNION SELECT 2,3 UNION SELECT 5,6 ) d
GROUP BY a WITH ROLLUP;
a	SUM(a)	SUM(a)+1	CONCAT(SUM(a),'x')	SUM(a)+SUM(a)	SUM(a)
1	1	2	1x	2	1
2	2	3	2x	4	2
5	5	6	5x	10	5
NULL	8	9	8x	16	8
DROP TABLE t1;
492 493 494 495 496 497 498 499 500 501 502 503 504 505
CREATE TABLE t1 (a int(11));
INSERT INTO t1 VALUES (1),(2);
SELECT a, a+1, SUM(a) FROM t1 GROUP BY a WITH ROLLUP;
a	a+1	SUM(a)
1	2	1
2	3	2
NULL	NULL	3
SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP;
a+1
2
3
NULL
SELECT a+SUM(a) FROM t1 GROUP BY a WITH ROLLUP;
a+SUM(a)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
506
2
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
4
NULL
SELECT a, a+1 as b FROM t1 GROUP BY a WITH ROLLUP HAVING b > 2;
a	b
2	3
SELECT a, a+1 as b FROM t1 GROUP BY a WITH ROLLUP HAVING a IS NULL;
a	b
NULL	NULL
SELECT a, a+1 as b FROM t1 GROUP BY a WITH ROLLUP HAVING b IS NULL;
a	b
NULL	NULL
SELECT IFNULL(a, 'TEST') FROM t1 GROUP BY a WITH ROLLUP;
IFNULL(a, 'TEST')
1
2
TEST
CREATE TABLE t2 (a int, b int);
INSERT INTO t2 VALUES
(1,4),
(2,2), (2,2),
(4,1), (4,1), (4,1), (4,1),
(2,1), (2,1);
SELECT a,b,SUM(b) FROM t2 GROUP BY a,b WITH ROLLUP;
a	b	SUM(b)
1	4	4
1	NULL	4
2	1	2
2	2	4
2	NULL	6
4	1	4
4	NULL	4
NULL	NULL	14
SELECT a,b,SUM(b), a+b as c FROM t2
GROUP BY a,b WITH ROLLUP HAVING c IS NULL;
a	b	SUM(b)	c
1	NULL	4	NULL
2	NULL	6	NULL
4	NULL	4	NULL
NULL	NULL	14	NULL
SELECT IFNULL(a, 'TEST'), COALESCE(b, 'TEST') FROM t2 
GROUP BY a, b WITH ROLLUP;
IFNULL(a, 'TEST')	COALESCE(b, 'TEST')
1	4
1	TEST
2	1
2	2
2	TEST
4	1
4	TEST
TEST	TEST
DROP TABLE t1,t2;
558 559 560 561 562 563 564 565 566 567 568 569
CREATE TABLE t1 (a INT(10) NOT NULL, b INT(10) NOT NULL);
INSERT INTO t1 VALUES (1, 1);
INSERT INTO t1 VALUES (1, 2);
SELECT a, b, a AS c, COUNT(*) AS count FROM t1 GROUP BY a, b, c WITH ROLLUP;
a	b	c	count
1	1	1	1
1	1	NULL	1
1	2	1	1
1	2	NULL	1
1	NULL	NULL	2
NULL	NULL	NULL	2
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
570 571 572 573 574 575 576 577 578 579 580 581
CREATE TABLE t1 (a int(11) NOT NULL);
INSERT INTO t1 VALUES (1),(2);
SELECT * FROM (SELECT a, a + 1, COUNT(*) FROM t1 GROUP BY a WITH ROLLUP) t;
a	a + 1	COUNT(*)
1	2	1
2	3	1
NULL	NULL	2
SELECT * FROM (SELECT a, LENGTH(a), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP) t;
a	LENGTH(a)	COUNT(*)
1	1	1
2	1	1
NULL	NULL	2
sergefp@mysql.com's avatar
sergefp@mysql.com committed
582
DROP TABLE t1;
583 584 585 586 587 588 589 590 591 592 593 594
create table t1 ( a varchar(9), b int );
insert into t1 values('a',1),(null,2);
select a, max(b) from t1 group by a with rollup;
a	max(b)
NULL	2
a	1
NULL	2
select distinct a, max(b) from t1 group by a with rollup;
a	max(b)
NULL	2
a	1
drop table t1;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
create table t1 (a varchar(22) not null , b int);
insert into t1 values ("2006-07-01 21:30", 1), ("2006-07-01 23:30", 10);
select left(a,10), a, sum(b) from t1 group by 1,2 with rollup;
left(a,10)	a	sum(b)
2006-07-01	2006-07-01 21:30	1
2006-07-01	2006-07-01 23:30	10
2006-07-01	NULL	11
NULL	NULL	11
select left(a,10) x, a, sum(b) from t1 group by x,a with rollup;
x	a	sum(b)
2006-07-01	2006-07-01 21:30	1
2006-07-01	2006-07-01 23:30	10
2006-07-01	NULL	11
NULL	NULL	11
drop table t1;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 
VALUES (2,10),(3,30),(2,40),(1,10),(2,30),(1,20),(2,10);
SELECT a, SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
a	SUM(b)
1	30
2	90
3	30
NULL	150
SELECT DISTINCT a, SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
a	SUM(b)
1	30
2	90
3	30
NULL	150
SELECT a, b, COUNT(*) FROM t1 GROUP BY a,b WITH ROLLUP;
a	b	COUNT(*)
1	10	1
1	20	1
1	NULL	2
2	10	2
2	30	1
2	40	1
2	NULL	4
3	30	1
3	NULL	1
NULL	NULL	7
SELECT DISTINCT a, b, COUNT(*) FROM t1 GROUP BY a,b WITH ROLLUP;
a	b	COUNT(*)
1	10	1
1	20	1
1	NULL	2
2	10	2
2	30	1
2	40	1
2	NULL	4
3	30	1
3	NULL	1
NULL	NULL	7
SELECT 'x', a, SUM(b) FROM t1 GROUP BY 1,2 WITH ROLLUP;
x	a	SUM(b)
x	1	30
x	2	90
x	3	30
x	NULL	150
NULL	NULL	150
SELECT DISTINCT 'x', a, SUM(b) FROM t1 GROUP BY 1,2 WITH ROLLUP;
x	a	SUM(b)
x	1	30
x	2	90
x	3	30
x	NULL	150
NULL	NULL	150
SELECT DISTINCT 'x', a, SUM(b) FROM t1 GROUP BY 1,2 WITH ROLLUP;
x	a	SUM(b)
x	1	30
x	2	90
x	3	30
x	NULL	150
NULL	NULL	150
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
670
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
CREATE TABLE t1(id int, type char(1));
INSERT INTO t1 VALUES
(1,"A"),(2,"C"),(3,"A"),(4,"A"),(5,"B"),
(6,"B"),(7,"A"),(8,"C"),(9,"A"),(10,"C");
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT type FROM t1 GROUP BY type WITH ROLLUP;
type
A
B
C
NULL
SELECT type FROM v1 GROUP BY type WITH ROLLUP;
type
A
B
C
NULL
EXPLAIN SELECT type FROM v1 GROUP BY type WITH ROLLUP;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
690
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	10	Using filesort
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
691
DROP VIEW v1;
konstantin@mysql.com's avatar
konstantin@mysql.com committed
692
DROP TABLE t1;
693 694 695 696 697 698
CREATE TABLE t1 (a int(11) NOT NULL);
INSERT INTO t1 VALUES (1),(2);
CREATE VIEW v1 AS
SELECT a, LENGTH(a), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
DESC v1;
Field	Type	Null	Key	Default	Extra
699 700
a	bigint(11)	YES		NULL	
LENGTH(a)	bigint(10)	YES		NULL	
701 702 703 704 705 706 707 708
COUNT(*)	bigint(21)	NO		0	
SELECT * FROM v1;
a	LENGTH(a)	COUNT(*)
1	1	1
2	1	1
NULL	NULL	2
DROP VIEW v1;
DROP TABLE t1;
709 710 711 712 713 714 715 716 717
CREATE TABLE t1 (a int, KEY (a));
INSERT INTO t1 VALUES (3), (1), (4), (1), (3), (1), (1);
SELECT * FROM (SELECT a, SUM(a) FROM t1 GROUP BY a WITH ROLLUP) as t;
a	SUM(a)
1	4
3	6
4	4
NULL	14
DROP TABLE t1;
718 719 720 721 722 723 724 725 726 727 728
#
# Bug#31095: Unexpected NULL constant caused server crash.
#
create table t1(a int);
insert into t1 values (1),(2),(3);
select count(a) from t1 group by null with rollup;
count(a)
3
3
drop table t1;
##############################################################