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))
Related
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
I have the following tables:
In other words I have a set of customers I follow throughout time. I have a column of the date of their first purchase, and another column with another purchase they made at another time.
I want to make a column which specifies the time period in which the specific purchase was made. The time periods should be defined such that a new period starts the 20th each month and the first period for each customer should therefore be the day they made their first purchase and until the 20th.
This is illustrated below:
What I need:
I have tried to implement this via a handful of if statements like:
WHEN DAY(c.created)<21 and DAY(s.created)<21 and year(c.created)-year(s.created)=0
THEN MONTH(c.created)-MONTH(s.created)+1
WHEN DAY(c.created)>20 and DAY(s.created)<21 and year(c.created)-year(s.created)=0
THEN MONTH(c.created)-MONTH(s.created)+2
and so on.
I want to know if there is an easy(er) and simple(r) way to handle this problem?
I work in MySQL Workbench 6.3 CE
The date of the first day of the calendar month in which a customer made a purchase can be found like this:
DATE_FORMAT(First_purchase , '%Y-%m-01')
So, the date of the first day of your fiscal month, which starts on the 20th of each calendar month, can be found like this.
DATE_FORMAT(First_purchase - INTERVAL 19 DAY, '%Y-%m-01') + INTERVAL 19 DAY
Then, you can use TIMESTAMPDIFF as follows to get the number of months between two of these sorts of numbers.
TIMESTAMPDIFF(
MONTH,
DATE_FORMAT(First_purchase - INTERVAL 19 DAY, '%Y-%m-01') + INTERVAL 19 DAY,
DATE_FORMAT(Date_created - INTERVAL 19 DAY, '%Y-%m-01') + INTERVAL 19 DAY) + 1
That will get you the number of the fiscal month, starting with First_purchase, in which Date_created is found, where the first fiscal month has the number 1.
Using native date arithmetic in place of DAY(), MONTH(), and YEAR() functions is much more likely to keep working over ends of years and in leap years and all that.
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
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;
I want to select all dates within the last week.
But not by simply counting the current date - 7, as all posts Ive come across suggest.
This is how I actually have it for the SUM now....
SELECT SUM(total) FROM payforms WHERE user_id = 1 GROUP BY WEEK(date)
This gives me a nice total...
But I want to retrieve all individual records within the last week.
So I can use a BETWEEN query....but how do I get it to look in the current week.
Example...
Tuesday I want it to only find values from Sun, Mon, Tue.
On wednesday, I want it to find Sun, Mon, Tue, Wed.
On Saturday it finds the whole previous week. etc
So to be more clear....
I dont want it to find last Monday, on a monday.
On Mondays it should only display Mondays, if you get what I mean.
Can I do this??
Thanks
select * from payforms where yearweek(date) = yearweek(now());
Although I think MySQL weeks start on Sunday
You can make use of the WEEKDAY date function, which returns the day index of the week.
If I understand your question correctly, you could use this query:
SELECT date, SUM(total)
FROM payforms
WHERE date >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY
GROUP BY date
Please see fiddle here.
This will SUM all totals for every day of the current week, starting on last Sunday. If you want it to start on last Monday you can use this:
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY