简体   繁体   English

如何在单个MySQL查询中选择多个平均值?

[英]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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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