简体   繁体   中英

Calculate average column value per day

I have the following table structure: Value (stores random integer values), Datetime` (stores purchased orders datetimes).

How would I get the average value from all Value rows across a full day?

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 :

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

Looks like a simple AVG task:

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. 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')

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