开发者

MySQL Single Row Returned From Temporary Table

开发者 https://www.devze.com 2023-01-25 19:21 出处:网络
I am running the following queries against a database: CREATE TEMPORARY TABLE med_error_third_party_tmp

I am running the following queries against a database:

CREATE TEMPORARY TABLE med_error_third_party_tmp
SELECT `med_error_category`.description AS category, `med_error_third_party_category`.error_count AS error_count
FROM
    `med_error_category` INNER JOIN `med_error_third_party_category` ON med_error_category.`id` = `med_error_third_party_category`.`category`
WHERE
    year = 2003
GROUP BY `med_error_category`.id;

The only problem is that when I create the temporary table and do a select * on it then it returns multiple rows, but the query above only returns one row. It seems to always return a single row unless I specify a GROUP BY, but then it returns a percentage of 1.0 like it should with a GROUP BY.

SELECT开发者_如何学C category, 
       error_count/SUM(error_count) AS percentage
  FROM med_error_third_party_tmp;

Here are the server specs:

Server version: 5.0.77

Protocol version: 10

Server: Localhost via UNIX socket

Does anybody see a problem with this that is causing the problem?


Standard SQL requires you to specify a GROUP BY clause if any column is not wrapped in an aggregate function (IE: MIN, MAX, COUNT, SUM, AVG, etc), but MySQL supports "hidden columns in the GROUP BY" -- which is why:

SELECT category, 
       error_count/SUM(error_count) AS percentage
  FROM med_error_third_party_tmp;

...runs without error. The problem with the functionality is that because there's no GROUP BY, the SUM is the SUM of the error_count column for the entire table. But the other column values are completely arbitrary - they can't be relied upon.

This:

SELECT category, 
       error_count/(SELECT SUM(error_count)
                      FROM med_error_third_party_tmp) AS percentage
  FROM med_error_third_party_tmp;

...will give you a percentage on a per row basis -- category values will be duplicated because there's no grouping.

This:

  SELECT category, 
         SUM(error_count)/x.total AS percentage
    FROM med_error_third_party_tmp
    JOIN (SELECT SUM(error_count) AS total
            FROM med_error_third_party_tmp) x 
GROUP BY category

...will gives you a percentage per category of the sum of the categories error_count values vs the sum of the error_count values for the entire table.


another way to do it - without the temp table as seperate item...

select category, error_count/sum(error_count) "Percentage"
from (SELECT mec.description category
          ,  metpc.error_count 
      FROM med_error_category mec
         , med_error_third_party_category metpc
      WHERE mec.id = metpc.category
      AND year = 2003 
      GROUP BY mec.id
     );

i think you will notice that the percentage is unchanging over the categories. This is probably not what you want - you probably want to group the errors by category as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消