mirror of
https://github.com/MariaDB/server.git
synced 2025-01-31 19:11:46 +01:00
4ad51359c1
(Includes merge of arena code in 4.1 and 5.0) BitKeeper/etc/ignore: auto-union BitKeeper/etc/logging_ok: auto-union VC++Files/sql/mysqld.dsp: Auto merged client/mysql.cc: Auto merged client/mysqltest.c: Auto merged include/my_global.h: Auto merged include/my_sys.h: Auto merged include/mysql.h: Auto merged include/mysql_com.h: Auto merged innobase/row/row0sel.c: Auto merged libmysql/client_settings.h: Auto merged libmysql/libmysql.c: Auto merged libmysqld/Makefile.am: Auto merged libmysqld/examples/Makefile.am: Auto merged libmysqld/lib_sql.cc: Auto merged myisam/mi_check.c: Auto merged myisam/myisamchk.c: Auto merged myisam/sort.c: Auto merged mysql-test/r/connect.result: Auto merged mysql-test/r/ctype_recoding.result: Auto merged mysql-test/r/ctype_ucs.result: Auto merged mysql-test/r/func_in.result: Auto merged mysql-test/r/func_like.result: Auto merged mysql-test/r/gis.result: Auto merged mysql-test/r/having.result: Auto merged mysql-test/r/heap.result: Auto merged mysql-test/r/join.result: Auto merged mysql-test/r/key.result: Auto merged mysql-test/r/lowercase_table.result: Auto merged mysql-test/r/ndb_autodiscover.result: Auto merged mysql-test/r/null.result: Auto merged mysql-test/r/olap.result: Auto merged mysql-test/r/order_by.result: Auto merged mysql-test/r/ps_1general.result: Auto merged mysql-test/r/ps_2myisam.result: Auto merged mysql-test/r/ps_3innodb.result: Auto merged mysql-test/r/ps_4heap.result: Auto merged mysql-test/r/ps_5merge.result: Auto merged mysql-test/r/ps_6bdb.result: Auto merged mysql-test/r/range.result: Auto merged mysql-test/r/rename.result: Auto merged mysql-test/r/show_check.result: Auto merged mysql-test/r/subselect.result: Auto merged mysql-test/r/union.result: Auto merged mysql-test/r/variables.result: Auto merged mysql-test/t/alter_table.test: Auto merged mysql-test/t/null.test: Auto merged mysql-test/t/ps_1general.test: Auto merged mysql-test/t/rpl_charset.test: Auto merged mysql-test/t/rpl_heap.test: Auto merged mysql-test/t/rpl_relayrotate.test: Auto merged mysql-test/t/subselect.test: Auto merged mysql-test/t/variables.test: Auto merged netware/mysql_test_run.c: Auto merged scripts/make_binary_distribution.sh: Auto merged scripts/mysql_create_system_tables.sh: Auto merged scripts/mysql_fix_privilege_tables.sql: Auto merged scripts/mysql_install_db.sh: Auto merged sql/ha_berkeley.cc: Auto merged sql/ha_innodb.cc: Auto merged sql/ha_innodb.h: Auto merged sql/ha_myisam.cc: Auto merged sql/handler.cc: Auto merged sql/handler.h: Auto merged sql/item.h: Auto merged sql/item_cmpfunc.h: Auto merged sql/item_create.cc: Auto merged sql/item_create.h: Auto merged sql/item_func.cc: Auto merged sql/item_func.h: Auto merged sql/item_timefunc.cc: Auto merged sql/item_timefunc.h: Auto merged sql/lex.h: Auto merged sql/lock.cc: Auto merged sql/log_event.cc: Auto merged sql/mysql_priv.h: Auto merged sql/opt_sum.cc: Auto merged sql/protocol.cc: Auto merged sql/records.cc: Auto merged sql/repl_failsafe.cc: Auto merged sql/set_var.cc: Auto merged sql/set_var.h: Auto merged sql/slave.cc: Auto merged sql/sql_acl.cc: Auto merged sql/sql_acl.h: Auto merged sql/sql_db.cc: Auto merged sql/sql_delete.cc: Auto merged sql/sql_insert.cc: Auto merged sql/sql_list.h: Auto merged sql/sql_load.cc: Auto merged sql/sql_rename.cc: Auto merged sql/sql_select.h: Auto merged sql/sql_show.cc: Auto merged sql/sql_string.h: Auto merged sql/table.cc: Auto merged sql-common/client.c: Auto merged client/mysqlbinlog.cc: Merge with 4.1 configure.in: Merge with 4.1 include/mysqld_error.h: Add new error message (1) from 4.1 mysql-test/mysql-test-run.sh: Merge with 4.1 mysql-test/r/func_gconcat.result: Merge with 4.1 mysql-test/r/func_if.result: Merge with 4.1 mysql-test/r/grant.result: Merge with 4.1 mysql-test/r/join_outer.result: Merge with 4.1 mysql-test/r/rpl_charset.result: Merge with 4.1 (This has to be fixed before pushing) mysql-test/r/system_mysql_db.result: Merge with 4.1. Added collation to new privileges mysql-test/t/grant.test: Merge with 4.1 mysql-test/t/grant_cache.test: Merge with 4.1 mysql-test/t/show_check.test: Merge with 4.1 sql/Makefile.am: Merge with 4.1 sql/item.cc: Merge with 4.1 sql/item_cmpfunc.cc: Merge with 4.1 (arena code) sql/item_subselect.cc: Merge with 4.1 sql/item_subselect.h: Merge with 4.1 sql/item_sum.cc: Merge with 4.1 sql/item_sum.h: Merge with 4.1 sql/log.cc: Merge with 4.1 (Remove code that is not relevant for 5.0) sql/mysqld.cc: Merge with 4.1 sql/opt_range.cc: Merge with 4.1 sql/share/czech/errmsg.txt: Merge with 4.1 sql/share/danish/errmsg.txt: Merge with 4.1 sql/share/dutch/errmsg.txt: Merge with 4.1 sql/share/english/errmsg.txt: Merge with 4.1 sql/share/estonian/errmsg.txt: Merge with 4.1 sql/share/french/errmsg.txt: Merge with 4.1 sql/share/german/errmsg.txt: Merge with 4.1 sql/share/greek/errmsg.txt: Merge with 4.1 sql/share/hungarian/errmsg.txt: Merge with 4.1 sql/share/italian/errmsg.txt: Merge with 4.1 sql/share/japanese/errmsg.txt: Merge with 4.1 sql/share/korean/errmsg.txt: Merge with 4.1 sql/share/norwegian-ny/errmsg.txt: Merge with 4.1 sql/share/norwegian/errmsg.txt: Merge with 4.1 sql/share/polish/errmsg.txt: Merge with 4.1 sql/share/portuguese/errmsg.txt: Merge with 4.1 sql/share/romanian/errmsg.txt: Merge with 4.1 sql/share/russian/errmsg.txt: Merge with 4.1 sql/share/serbian/errmsg.txt: Merge with 4.1 sql/share/slovak/errmsg.txt: Merge with 4.1 sql/share/spanish/errmsg.txt: Merge with 4.1 sql/share/swedish/errmsg.txt: Merge with 4.1 sql/share/ukrainian/errmsg.txt: Merge with 4.1 sql/sql_base.cc: Merge with 4.1 sql/sql_class.cc: Merge with 4.1 Use arena code from 4.1 sql/sql_class.h: Merge with 4.1 Use arena code from 4.1 sql/sql_derived.cc: Merge with 4.1 sql/sql_lex.cc: Merge with 4.1 sql/sql_lex.h: Merge with 4.1 sql/sql_parse.cc: Merge with 4.1 sql/sql_prepare.cc: Merge with 4.1 sql/sql_select.cc: Merge with 4.1 sql/sql_table.cc: Merge with 4.1 sql/sql_union.cc: Merge with 4.1 sql/sql_yacc.yy: Merge with 4.1 sql/tztime.cc: Merge with 4.1 tests/client_test.c: Merge with 4.1
309 lines
9.2 KiB
Text
309 lines
9.2 KiB
Text
drop table if exists t1,t2;
|
|
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)
|
|
Calculator 275 68.7500
|
|
Computer 6900 1380.0000
|
|
Phone 10 10.0000
|
|
TV 600 120.0000
|
|
NULL 7785 519.0000
|
|
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
|
|
explain extended select product, country_id , year, sum(profit) from t1 group by product, country_id, year with rollup;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using temporary; Using filesort
|
|
Warnings:
|
|
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
|
|
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(*)
|
|
Calculator:1 :1999: 2 50.00
|
|
Calculator:1 :2000: 2 75.00
|
|
Calculator:1 NULL 2 62.50
|
|
Calculator:2 :2000: 2 75.00
|
|
Calculator:2 NULL 2 75.00
|
|
Computer:1 :1999: 2 1350.00
|
|
Computer:1 :2000: 2 1500.00
|
|
Computer:1 NULL 2 1400.00
|
|
Computer:2 :2000: 2 1350.00
|
|
Computer:2 NULL 2 1350.00
|
|
Phone:3 :2003: 2 10.00
|
|
Phone:3 NULL 2 10.00
|
|
TV:1 :1999: 2 125.00
|
|
TV:1 :2000: 2 150.00
|
|
TV:1 NULL 2 133.33
|
|
TV:2 :2000: 2 100.00
|
|
TV:2 NULL 2 100.00
|
|
NULL NULL 2 519.00
|
|
select product, sum(profit)/count(*) from t1 group by product with rollup;
|
|
product sum(profit)/count(*)
|
|
Calculator 68.75
|
|
Computer 1380.00
|
|
Phone 10.00
|
|
TV 120.00
|
|
NULL 519.00
|
|
select left(product,4) as prod, sum(profit)/count(*) from t1 group by prod with rollup;
|
|
prod sum(profit)/count(*)
|
|
Calc 68.75
|
|
Comp 1380.00
|
|
Phon 10.00
|
|
TV 120.00
|
|
NULL 519.00
|
|
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(*)
|
|
Calculator:1 2 62.50
|
|
Calculator:2 2 75.00
|
|
Computer:1 2 1400.00
|
|
Computer:2 2 1350.00
|
|
Phone:3 2 10.00
|
|
TV:1 2 133.33
|
|
TV:2 2 100.00
|
|
NULL 2 519.00
|
|
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)
|
|
:Calculator: 275 68.7500
|
|
:Computer: 6900 1380.0000
|
|
:Phone: 10 10.0000
|
|
:TV: 600 120.0000
|
|
:TV: 7785 519.0000
|
|
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;
|
|
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;
|
|
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;
|