简体   繁体   中英

How to select multiple averages in a single MySQL query?

I need to select multiple time averages from a table. I can do it with multiple queries, but I want to know how to unify all in a single query.

For example, I have this table:

------------------------------------------------
| id | number |   name   |    date    | duration |
-------------------------------------------------
| 1  |    2   | SOMENAME | 2019-07-01 | 02:34:33 |
| 2  |    3   | SOMETHIN | 2019-03-05 | 01:54:31 |
| 3  |    2   | ANEWNAME | 2018-09-21 | 04:17:43 |
| 4  |    2   | SOMENAME | 2019-07-11 | 02:33:07 |
-------------------------------------------------

And my queries:

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date LIKE '%2019-07%';
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59';
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59' and name like '%SOMENAME%' and duration > '00:00:01' group by name;

I have tried this, without success:

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and (date LIKE '%2019-07%' OR date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59' OR date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59' and name like '%SOMENAME%')

I would like to perform the same queries in a single one, adding a new column at the left of the average, with a custom value on it. E. g:

------------------------
|description | average  |
------------------------
| month avg  | 02:33:11 |
------------------------
|  year avg  | 02:17:19 |
------------------------
| year avg2  | 02:52:26 |
------------------------

Do you have any hint?

you can actually include the conditional statement in the average. eg

SEC_TO_TIME(AVG( IF(date LIKE '%2019-07%', TIME_TO_SEC(duration), NULL) )) as avgA, 
SEC_TO_TIME(AVG( IF(date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59', TIME_TO_SEC(duration), NULL) )) as avgB, 
SEC_TO_TIME(AVG( IF(date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59'  and name like '%SOMENAME%' and duration > '00:00:01', TIME_TO_SEC(duration), NULL) )) as avgC 

This will only average rows which match the date condition since null values are ignored in an average.

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(t1.duration))) t1_avg_duration,
       SEC_TO_TIME(AVG(TIME_TO_SEC(t2.duration))) t2_avg_duration,
       SEC_TO_TIME(AVG(TIME_TO_SEC(t3.duration))) t3_avg_duration
  FROM mytable as t1,mytable as t2,mytable as t3
 where t1.number='2'
   and t1.number = t2.number
   and t2.number = t3.number
   and t1.date LIKE '%2019-07%'
   and t2.date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59'
   and t3.date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59' 
   and t3.name like '%SOMENAME%' and t3.duration > '00:00:01' group by t3.name;

Or simply union all of your queries if multiple line is ok for you

SELECT 'Custom text' as text1,SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date LIKE '%2019-07%';
union
SELECT 'Custom text' as text2,SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59';
union
SELECT 'Custom text' as text3,SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) FROM mytable where number='2' and date BETWEEN '2018-07-31 23:59:59' AND '2019-07-31 23:59:59' and name like '%SOMENAME%' and duration > '00:00:01' group by name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM