diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index aaa03f2668a..d990760d39b 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -393,3 +393,26 @@ One Two sum(Four) 1 2 16 1 3 16 drop table if exists t1; +drop table if exists t1,t2; +create table t1 (id integer primary key not null auto_increment, gender char(1)); +insert into t1 values(NULL, 'M'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'M'); +create table t2 (user_id integer not null, timestamp datetime); +insert into t2 values (1, sysdate()); +insert into t2 values (2, sysdate()); +insert into t2 values (1, sysdate()); +insert into t2 values (3, sysdate()); +insert into t2 values (4, sysdate()); +insert into t2 values (4, sysdate()); +select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender; +gender dist_count percentage +F 3 60.00 +M 1 20.00 +select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender order by percentage; +gender dist_count percentage +M 1 20.00 +F 3 60.00 +drop table t1,t2; diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test index 171b5510227..09a2bee0d0c 100644 --- a/mysql-test/t/group_by.test +++ b/mysql-test/t/group_by.test @@ -299,3 +299,20 @@ insert into t1 values (1,3,3,4); insert into t1 values (1,3,4,4); select One, Two, sum(Four) from t1 group by One,Two; drop table if exists t1; +drop table if exists t1,t2; +create table t1 (id integer primary key not null auto_increment, gender char(1)); +insert into t1 values(NULL, 'M'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'F'); +insert into t1 values(NULL, 'M'); +create table t2 (user_id integer not null, timestamp datetime); +insert into t2 values (1, sysdate()); +insert into t2 values (2, sysdate()); +insert into t2 values (1, sysdate()); +insert into t2 values (3, sysdate()); +insert into t2 values (4, sysdate()); +insert into t2 values (4, sysdate()); +select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender; +select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender order by percentage; +drop table t1,t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 6e819150692..3feb0fdddc3 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -135,7 +135,7 @@ static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables); static void calc_group_buffer(JOIN *join,ORDER *group); static bool alloc_group_fields(JOIN *join,ORDER *group); static bool make_sum_func_list(JOIN *join,List &fields); -static bool change_to_use_tmp_fields(List &func); +static bool change_to_use_tmp_fields(List &func, bool change=false); static bool change_refs_to_tmp_fields(THD *thd, List &func); static void init_tmptable_sum_functions(Item_sum **func); static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table); @@ -788,7 +788,7 @@ mysql_select(THD *thd,TABLE_LIST *tables,List &fields,COND *conds, tmp_table=tmp_table2; join.join_tab[0].table=0; // Table is freed - if (change_to_use_tmp_fields(all_fields)) // No sum funcs anymore + if (change_to_use_tmp_fields(all_fields,true)) // No sum funcs anymore goto err; join.tmp_table_param.field_count+=join.tmp_table_param.sum_func_count; join.tmp_table_param.sum_func_count=0; @@ -6764,7 +6764,7 @@ make_sum_func_list(JOIN *join,List &fields) */ static bool -change_to_use_tmp_fields(List &items) +change_to_use_tmp_fields(List &items, bool change) { List_iterator it(items); Item *item_field,*item; @@ -6776,6 +6776,11 @@ change_to_use_tmp_fields(List &items) continue; if (item->type() == Item::FIELD_ITEM) { + if (change) + { + ((Item_field*) item)->result_field->null_ptr=0; + item->maybe_null=0; + } ((Item_field*) item)->field= ((Item_field*) item)->result_field; }