简体   繁体   English

如何在mysql中获得一个月的平均值?

[英]how to get week average from month in mysql?

i have day, month, year, value columns in one table, here i need to get every week average value in one month. 我在一个表中有日,月,年,值列,这里我需要在一个月内得到每周的平均值。 how to get that. 怎么做到这一点。 please help me regarding this. 请帮我解决这个问题。

select avg(value) from table group by month 

gives month average. 给出月平均值。

select avg(value) from table group by day

gives day average. 给出平均日数。 but how to get week average from month field. 但如何从月场获得周平均值。

You can't "get weeks from a month" as one is not a subset of the other. 你不能“从一个月开始数周”,因为一个人不是另一个人的子集。
The number of days (and hence weeks) in 1 month varies from month to month and only in non-leap years - and in February only - are there exactly 4 weeks in a month. 1个月内的天数(以及周数)因月而异,仅在非闰年 - 仅在2月份 - 一个月恰好有4周。

You should use the original date field and use a date function to limit/group the data by week. 您应该使用原始日期字段并使用日期函数按周限制/分组数据。
get the week in mySQL with 用mySQL获取一周
WEEK(timestamp)
or 要么
YEARWEEK(timestamp)
or 要么
WEEKOFYEAR(NOW())
or 要么
DATE_FORMAT($yourDate, \\'%X %V\\') as week

There might be a better way, but my first thought is to write a query that calculates the week of the year for each day and groups them. 可能有更好的方法,但我的第一个想法是编写一个查询,计算每一天的一周,并对它们进行分组。 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekofyear http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekofyear

Something like: 就像是:

SELECT avg(value) FROM table GROUP BY WEEKOFYEAR(CONCAT(year, '-', month, '-', day))

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

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