MySQL query daily avg value over years for the same day - mysql

I've got a MySQL database filled with weather data, e.g. mean temperature value for every day. I would like query for the average of these values for every day the last five years.
for example:
2019-06-04 20.04
2018-06-04 18.42
2017-06-04 19.21
2016-06-04 21.22
2015-06-04 17.19
query result should be: 19.216
For now I am able to get the avg for a specific day for the last years:
select date, avg(ta) from weatherdata where date like "20%-06-04";
But I am searching for an option to get the avg value for every day in a single query if possible.

Use GROUP BY.
SELECT MONTH(date) AS month, DAY(date) AS day, AVG(ta)
FROM weatherdata
GROUP BY month, day
ORDER BY month, day

Related

SQL query average over 100 days at a gap of 7 days

So, for this query I need to use 2 columns which are date and value. I need to query the average of value over 100 days at a gap of 7 days, in layman terms I want the average value for all 7 days in a week over a time span of 100 days.
Value in my database depicts the revenue. For example, what I am trying to get is the average of revenue over last 100 wednesdays. Similarly for every 7 days in the week.
If I am correct this is the query you are looking for:
SELECT WEEKDAY(RecordDate), AVG(revenue)
FROM Table
GROUP BY WEEKDAY(RecordDate)
WHERE RecordDate BETWEEN DATE_SUB(NOW(), INTERVAL -700 DAY) AND NOW()

Double group by SQL for day of week and hour/minute

Every minute I have a value in my database. Each record has a timestamp, and a value.
"30695","2015-09-06 18:10:09","693"
I want to get the average value for each minute for each day of the week, but I'm not sure how to build a query for this.
My thinking is that I first need to group all the records by DATEPART(weekday,[time]), which will put all my data into Sunday, Monday, Tuesday, etc. Next, I would group by minute, rounded. (So 2015/11/30 06:41:28 would get grouped with 2015/11/23 06:41:56 (both dates are Mondays)). Then, take the average of the value at each minute--7*24*60 = 10,080 total values.
There is no datepart() function in mysql, but you can use dayofweek() to get the day of the week and minute() to get the minute part:
select dayofweek(timestamp) as day, minute(timestamp) as minute, avg(value_field) as average
from table
group by dayofweek(timestamp), minute(timestamp);
Got it working, adapted from Shadow's answer, which wasn't quite right. Here's my query (MySQL 5.5)
SELECT weekday(timestamp) as day_of_week, avg(avg) as average, DATE_FORMAT(timestamp,'%H:%i') as hour_time
FROM audio
GOUP BY hour_time, day_of_week
ORDER BY `day_of_week` ASC

SQL Syntax run operations in between records?

I have a mysql database with a table in it. This table consists of the some of the following information. It has values in one column with months Jan-May. So five months. On the adjacent column, there are "Counts" with integer values to each month. Bear in mind that there can be duplicate values of the months. So, for example, a snippet of the table could read
January | 5
January | 10
February | 1
March | 20
April | 23
April | 34
April | 43
May | 9
There are a lot more records (160). Say the average of the month is running some sql command like
select month, avg(count) from tablename group by month. However, this divides the sum of counts for each month by the number of records. A true average would divide the sum of the counts by the number of days in each month. So I have the following statements,
select month, sum(count)/31 from trendsummary.traffictype where month like 'January';
select month, sum(count)/28 from trendsummary.traffictype where month like 'February';
select month, sum(count)/31 from trendsummary.traffictype where month like 'March';
select month, sum(count)/30 from trendsummary.traffictype where month like 'April';
select month, sum(count)/31 from trendsummary.traffictype where month like 'May';
This gives me the averages for the counts for each month. So the question is...what would be the syntax if I wanted an average of the averages of Jan-April? So... I want to have statements that would take the averages (based on the number of days of the month) for each of the months, and then take the average of the averages for January, February, March, And April and spit that value out? How would one go about this? Thanks!
you can try that :
select month, sum(count)/(31+28+31+30)
from trendsummary.traffictype
where month in ( 'January' , 'February','March','April' );
Union the selects and enclose them in parentheses and treat that as you data source as in this example:
select avg(*) from (
select month, sum(count)/31 as average from ...
union select ...
union select ...
)
remember that most sql engines will require to name the computed expression column like I did (as average) at least in the first select of all union selects.

MySQL Query to group by date range?

I've got a table in MySQL that shows me number of hours logged on a daily basis. I'm trying to build a view that will allow me to quickly group my data by blocks/range of days. The simplest case would be on a monthly basis which wouldn't be difficult. I could just select the date as "%y-%m" and then group by that column.
Ex:
select time_logged, date_format(start_date, '%Y-%m') AS `month_logged`
from work_log
group by month_logged
That works fine if I am just grouping by month. But my issue is that I need to group from the 13th of the month to the 12th of the following month (ex: July 13-Aug 12, Aug 13- Sept 12, etc).
Is there an easy way to do something like that in a single query/view? I can't seem to come up with a query that works for my needs, even playing with the different date field combinations.
Subtract 13 days and do the grouping you are doing now:
select time_logged,
date_format(start_date - interval 12 day, '%Y-%m') AS `month_logged`
from work_log
group by month_logged;

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.
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
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
Something like:
SELECT avg(value) FROM table GROUP BY WEEKOFYEAR(CONCAT(year, '-', month, '-', day))