English 中文(简体)
Averaging a total in mySQL
原标题:
  • 时间:2009-11-09 14:43:41
  •  标签:
  • mysql

My table looks like

person_id  | car_id | miles
------------------------------
    1      |   1    |  100
    1      |   2    |  200
    2      |   3    | 1000 
    2      |   4    |  500

I need to total the miles for each person and then average those totals.

There are 2 people - person 1 drove 300 miles, person 2 drove 1500 miles.

(300+1500)/2 = 900 average number of miles driven per person.

Which is the same thing as totaling the number of miles and dividing by the number of people.

I cannot figure out a mySQL statement that will either give me the average across people or give me total the number of miles and the number of people so I can do the division.

最佳回答

Total per person:

SELECT person_id, SUM(miles) FROM table GROUP BY person_id

Average

SELECT SUM(miles) / COUNT(DISTINCT person_id) FROM table

These should work

问题回答

As soon as the person_id and miles are indexed, the fastest method will be this:

SELECT  SUM(miles) /
        (
        SELECT  COUNT(*)
        FROM    (
                SELECT  DISTINCT person_id
                FROM    mytable
                ) q
        )
FROM    mytable

This will allow using two separate indexes for miles and person_id, the first one being in no certain order, the second one using INDEX FOR GROUP BY.

The sum will be calculated in no certain order and requires only a single index scan, without table lookup.

The subquery will be executed once using INDEX FOR GROUP BY and cached.

The final division, hence, will be a single operation over a precalculated sum and precalculated COUNT.

SELECT AVG(`miles_per_person`)
FROM
   (SELECT SUM(`miles`) `miles_per_person`
      FROM `persons`
  GROUP BY `person_id`) `a`

Corrected: just as the other author:

select sum(miles)/count(distinct person_id) avg_miles from tablename;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签