MySQL Query to group by date range? - mysql

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;

Related

SQL query not returning all days from timestamp

I need to run an sql query grouping each data by date. My column in named created and is a timestamp. The query works fine using YEAR(created), MONTH(created), WEEK(created) and shows all the results, however, with day it shows only a range. How can I solve this?
query with YEAR or MONTH or WEEK(created)
SELECT created
FROM mimesi_indexer.meta_served_clips
GROUP BY YEAR(created) //<-- that can be either YEAR, MONTH or WEEK
This query returns all data divided into year, months or weeks between 3rd of March 2017 and today
query with DAY(created)
SELECT created
FROM mimesi_indexer.meta_served_clips
GROUP BY DAY(created)
This query, however, returns all data divided days only between 3rd of March 2017 and 31st of March
The answer is to use DATE(created) instead of DAY(created). Because Day() returns the day of the month (1-31) not the date.

MySQL Date For Monthly Reporting

I need some help with running a query at month end. Each 1st working day of a month may differ, and therefore I may only be at work on the 3rd of a given month.
I am trying to figure out what my WHERE statement would look like to select data for the current month, unless it is:
1st of a month, then it will need to select everything from the previous month
1st working day of a month, which could be the 3rd. It will then also need to select the previous month's data.
These are two scenarios I am currently playing with, and don't have data to test it with as yet.
I have thought about doing
WHERE
MONTH(action_date) = MONTH(DATE_SUB(CURDATE(),INTERVAL 1 DAY))
But this then also returns data from 2016.
I have also thought of doing
WHERE
action_date = DATE_SUB(CURDATE(),INTERVAL 1 DAY)
But this would not work if today was say Monday the 3rd.
I would appreciate any answers that would give me the best way of doing this
You could simply subtract a few more days or even a month from the date, as all you will actually get from the subtraction is a month anyway
MONTH(DATE_SUB(CURDATE(),INTERVAL 5 DAY))
OR
MONTH(DATE_SUB(CURDATE(),INTERVAL 1 MONTH))

Mysql query to fetch/store records in a specific month

I have this module where i am supposed to check the winners of bidding in the current month. be that October, November, December or whatever.
I run this query,
SELECT *
FROM auction_winners
WHERE MONTH('2015-10-16 00:00:00')
but it shows me everything, from all the months.
i believe even if this query works it will work for October only, i am looking for something that checks system current date month.
Just add your column with date:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH('2015-10-16 00:00:00')
or:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH(NOW())

Select dates within the last week, but not only last 7 days

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

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