简体   繁体   English

计算每天的平均列值

[英]Calculate average column value per day

I have the following table structure: Value (stores random integer values), Datetime` (stores purchased orders datetimes). 我具有以下表结构: Value (存储随机整数值),日期时间(存储购买的订单日期时间)。

How would I get the average value from all Value rows across a full day? 我如何从一整天的所有“ Value行中获取平均值?

I'm assuming the query would be something like the following 我假设查询将如下所示

SELECT  count(*) / 1
FROM  mytable
WHERE DateTime = date(now(), -1 DAY) 

You can GROUP BY the DATE part of DATETIME and use AVG aggregate function to find an average value for each group : 您可以在DATETIMEDATE部分进行GROUP BY ,并使用AVG聚合函数查找每个组的平均值:

SELECT AVG(`Value`)
     , DATE(`Datetime`)
FROM `mytable`
GROUP BY DATE(`Datetime`)

Looks like a simple AVG task: 看起来像一个简单的AVG任务:

SELECT `datetime`,AVG(`Value`) as AvgValue
FROM TableName
GROUP BY `datetime`

To find average of a specific day: 要查找特定日期的平均值:

SELECT `datetime`,AVG(`Value`) as AvgValue
FROM TableName
WHERE `datetime`=@MyDate
GROUP BY `datetime`

Or Simply: 或简单地说:

SELECT AVG(`Value`) as AvgValue
FROM TableName
WHERE `datetime`=@MyDate

Explanation: 说明:

AVG is an aggregate function used to find the average of a column. AVG是用于查找列AVG的聚合函数。 Read more here . 在这里阅读更多。

尝试一下它的工作...

Select AVG(value),convert(nvarchar,DateTime,103) from table group by convert(nvarchar,DateTime,103)

The following query will give you what u want.. 以下查询将为您提供所需的信息。

SELECT DATE_FORMAT(thedate_field, '%Y-%m-%d') as theday, avg (value)
FROM mytable group by
DATE_FORMAT(thedate_field, '%Y-%m-%d')

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

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