📄 olap.result
字号:
drop table if exists t1,t2;set @sav_dpi= @@div_precision_increment;set div_precision_increment= 5;show variables like 'div_precision_increment';Variable_name Valuediv_precision_increment 5create 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 275Computer 6900Phone 10TV 600select product, sum(profit) from t1 group by product with rollup;product sum(profit)Calculator 275Computer 6900Phone 10TV 600NULL 7785select product, sum(profit) from t1 group by 1 with rollup;product sum(profit)Calculator 275Computer 6900Phone 10TV 600NULL 7785select product, sum(profit),avg(profit) from t1 group by product with rollup;product sum(profit) avg(profit)Calculator 275 68.75000Computer 6900 1380.00000Phone 10 10.00000TV 600 120.00000NULL 7785 519.00000select product, country_id , year, sum(profit) from t1 group by product, country_id, year;product country_id year sum(profit)Calculator 1 1999 50Calculator 1 2000 75Calculator 2 2000 150Computer 1 1999 2700Computer 1 2000 1500Computer 2 2000 2700Phone 3 2003 10TV 1 1999 250TV 1 2000 150TV 2 2000 200select 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 50Calculator 1 2000 75Calculator 1 NULL 125Calculator 2 2000 150Calculator 2 NULL 150Calculator NULL NULL 275Computer 1 1999 2700Computer 1 2000 1500Computer 1 NULL 4200Computer 2 2000 2700Computer 2 NULL 2700Computer NULL NULL 6900Phone 3 2003 10Phone 3 NULL 10Phone NULL NULL 10TV 1 1999 250TV 1 2000 150TV 1 NULL 400TV 2 2000 200TV 2 NULL 200TV NULL NULL 600NULL NULL NULL 7785explain 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 Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using filesortWarnings: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 rollupselect product, country_id , sum(profit) from t1 group by product desc, country_id with rollup;product country_id sum(profit)TV 1 400TV 2 200TV NULL 600Phone 3 10Phone NULL 10Computer 1 4200Computer 2 2700Computer NULL 6900Calculator 1 125Calculator 2 150Calculator NULL 275NULL NULL 7785select 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 50Calculator 1 2000 75Calculator 1 NULL 125Calculator 2 2000 150Calculator 2 NULL 150select 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 150Calculator 2 NULL 150Calculator NULL NULL 275select product, country_id, count(*), count(distinct year) from t1 group by product, country_id;product country_id count(*) count(distinct year)Calculator 1 2 2Calculator 2 2 1Computer 1 3 2Computer 2 2 1Phone 3 1 1TV 1 3 2TV 2 2 1select 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 2Calculator 2 2 1Calculator NULL 4 2Computer 1 3 2Computer 2 2 1Computer NULL 5 2Phone 3 1 1Phone NULL 1 1TV 1 3 2TV 2 2 1TV NULL 5 2NULL NULL 15 3select 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 50Calculator 1 2000 75Calculator 1 NULL 125Computer 1 1999 2700Computer 1 2000 1500Computer 1 NULL 4200TV 1 1999 250TV 1 2000 150TV 1 NULL 400select 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 275Computer 1 1999 2700Computer 1 2000 1500Computer 1 NULL 4200Computer 2 2000 2700Computer 2 NULL 2700Computer NULL NULL 6900TV 1 1999 250TV 1 NULL 400TV NULL NULL 600NULL NULL NULL 7785select 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 7785select 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.00000Calculator:1 :2000: 2 75.00000Calculator:1 NULL 2 62.50000Calculator:2 :2000: 2 75.00000Calculator:2 NULL 2 75.00000Computer:1 :1999: 2 1350.00000Computer:1 :2000: 2 1500.00000Computer:1 NULL 2 1400.00000Computer:2 :2000: 2 1350.00000Computer:2 NULL 2 1350.00000Phone:3 :2003: 2 10.00000Phone:3 NULL 2 10.00000TV:1 :1999: 2 125.00000TV:1 :2000: 2 150.00000TV:1 NULL 2 133.33333TV:2 :2000: 2 100.00000TV:2 NULL 2 100.00000NULL NULL 2 519.00000select product, sum(profit)/count(*) from t1 group by product with rollup;product sum(profit)/count(*)Calculator 68.75000Computer 1380.00000Phone 10.00000TV 120.00000NULL 519.00000select left(product,4) as prod, sum(profit)/count(*) from t1 group by prod with rollup;prod sum(profit)/count(*)Calc 68.75000Comp 1380.00000Phon 10.00000TV 120.00000NULL 519.00000select 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.50000Calculator:2 2 75.00000Computer:1 2 1400.00000Computer:2 2 1350.00000Phone:3 2 10.00000TV:1 2 133.33333TV:2 2 100.00000NULL 2 519.00000select 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 150Calculator India NULL 150Calculator USA 1999 50Calculator USA 2000 75Calculator USA NULL 125Calculator NULL NULL 275Computer India 2000 2700Computer India NULL 2700Computer USA 1999 2700Computer USA 2000 1500Computer USA NULL 4200Computer NULL NULL 6900Phone Finland 2003 10Phone Finland NULL 10Phone NULL NULL 10TV India 2000 200TV India NULL 200TV USA 1999 250TV USA 2000 150TV USA NULL 400TV NULL NULL 600NULL NULL NULL 7785select product, `sum` from (select product, sum(profit) as 'sum' from t1 group by product with rollup) as tmp where product is null;product sumNULL 7785select 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);productComputerComputerComputerComputerComputerselect 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.75000:Computer: 6900 1380.00000:Phone: 10 10.00000:TV: 600 120.00000NULL 7785 519.00000select 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 1NULL 1SELECT t1.i, t2.i, COUNT(*) FROM t1,t2 GROUP BY t1.i,t2.i WITH ROLLUP;i i COUNT(*)100 100 1100 200 1100 NULL 2NULL NULL 2drop 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' );SELECTd.date AS day,COUNT(d.user_id) as sample,COUNT(next_day.user_id) AS not_cancelledFROM user_day dLEFT 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_cancelled2004-06-06 2 12004-06-07 1 0SELECTd.date AS day,COUNT(d.user_id) as sample,COUNT(next_day.user_id) AS not_cancelledFROM user_day dLEFT 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 dayWITH ROLLUP;day sample not_cancelled2004-06-06 2 12004-06-07 1 0NULL 3 1DROP TABLE user_day;CREATE TABLE t1 (a int, b int);INSERT INTO t1 VALUES(1,4),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -