sql - MySQL Single Row Returned From Temporary Table -
i running following queries against database:
create temporary table med_error_third_party_tmp select `med_error_category`.description category, `med_error_third_party_category`.error_count error_count `med_error_category` inner join `med_error_third_party_category` on med_error_category.`id` = `med_error_third_party_category`.`category` year = 2003 group `med_error_category`.id;
the problem when create temporary table , select * on returns multiple rows, query above returns 1 row. seems return single row unless specify group by, returns percentage of 1.0 should group by.
select category, error_count/sum(error_count) percentage med_error_third_party_tmp;
here server specs:
server version: 5.0.77
protocol version: 10
server: localhost via unix socket
does see problem causing problem?
standard sql requires specify group clause if column not wrapped in aggregate function (ie: min, max, count, sum, avg, etc), mysql supports "hidden columns in group by" -- why:
select category, error_count/sum(error_count) percentage med_error_third_party_tmp;
...runs without error. problem functionality because there's no group by, sum sum of error_count column entire table. other column values arbitrary - can't relied upon.
this:
select category, error_count/(select sum(error_count) med_error_third_party_tmp) percentage med_error_third_party_tmp;
...will give percentage on per row basis -- category values duplicated because there's no grouping.
this:
select category, sum(error_count)/x.total percentage med_error_third_party_tmp join (select sum(error_count) total med_error_third_party_tmp) x group category
...will gives percentage per category of sum of categories error_count values vs sum of error_count values entire table.
Comments
Post a Comment