I have searched and viewed a lot of answers on SO but even trying to apply all I found I get incorrect results.
I am trying to group my mysql results by week mon-sun, and by month from day 1 to last day of month
I have tried different types of grouping like:
GROUP BY YEARWEEK(data)
GROUP BY WEEK(data)
for weeks and
GROUP BY MONTH(data)
for months
all these grouping methods return uncorrect groupings. some results start in random days of the month or the week.
this is example query which get results from last month entries divided by week although the week grouping is not resulting in weeks from mon-sun.
SELECT *, count(id) AS count FROM leadz WHERE YEAR(data) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(data) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) GROUP BY YEARWEEK(data)
these are the week segments I get as results:
2016-07-01 2016-07-04 2016-07-16 2016-07-17 2016-07-25 2016-07-31
Next is a sample query of total entries grouped by month, again gropuing does not start from 1st of the month
SELECT *, count(id) AS count FROM leadz GROUP BY MONTH(data)
these are the month segments I get as result:
2016-04-18 2016-05-25 2016-06-01 2016-07-25 2016-08-02
Any help appreciated
For starting on monday you should use
YEARWEEK(data, 1)
or
WEEK(data,1)
see this for ref http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
so your select should be
SELECT *, count(id) AS count
FROM leadz
WHERE YEAR(data) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(data) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
GROUP BY YEARWEEK(data, 1)
Related
How do I get all records from a table from this week and the next 3 weeks, in total 4 weeks.
This will gave me this week.
SELECT *, FROM_UNIXTIME(date) as time FROM `events` WHERE
WEEK(FROM_UNIXTIME(date)) = WEEK(NOW())
AND MONTH(FROM_UNIXTIME(date)) = MONTH(NOW())
AND YEAR(FROM_UNIXTIME(date)) = YEAR(NOW())
Any ideas on how to get the data for the next 3 weeks.
Use a time range:
WHERE date BETWEEN
UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE(), INTERVAL(1-DAYOFWEEK(CURRENT_DATE())) DAY)) -- beginning of current week
AND UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE(), INTERVAL(1-DAYOFWEEK(CURRENT_DATE()) + 28) DAY)) -- beginning of 4 weeks after
I have the following table called vacations, where the employee id is displayed along with the start and end date of their vacations:
employee
start
end
1001
26/10/21
22/11/21
What I am looking for is to visualize the number of vacation days that each employee had, but separating them by month and without non-working days (Saturdays and Sundays).
For example, if you wanted to view the vacations for employee 1001, the following result should be displayed:
days
month
4
10
16
11
I have the following query that I have worked with:
SELECT id_employee,
EXTRACT(YEAR_MONTH FROM t.Date) as YearMonth,
COUNT(1) as Days
FROM (SELECT v.id_employee,
DATE_ADD(v.start, interval s.seq - 1 DAY) AS Date
FROM vacations v
CROSS JOIN seq_1_to_100 s
WHERE DATE_ADD(v.start, interval s.seq - 1 DAY) <= v.end
ORDER BY v.id_employee, , v.start, s.seq
) t
GROUP BY id_employee,
EXTRACT(YEAR_MONTH FROM t.Date)
With this query I separate the days between a range of two dates with their respective month, but how could I adapt it to stop considering Saturdays and Sundays? I'm working with MySQL 5.7 in phpMyAdmin
instead of count sum the compaarison of weekday function, which give what day it is .
But you should always save fates n a valid mysql manner 2021-10-28
SELECT id_employee,
EXTRACT(YEAR_MONTH FROM t.Date) as YearMonth,
SUM(WEEKDAY(`Date`) < 5) as Days
FROM (SELECT v.id_employee,
DATE_ADD(v.start, interval s.seq - 1 DAY) AS Date
FROM vacations v
CROSS JOIN seq_1_to_100 s
WHERE DATE_ADD(v.start, interval s.seq - 1 DAY) <= v.end
ORDER BY v.id_employee, v.start, s.seq
) t
GROUP BY id_employee,
EXTRACT(YEAR_MONTH FROM t.Date)
I have a table like this:
I need to sum how many messages were delivered per msisdn in last 8 weeks(but for each week) from date entered. Here is what I came up with:
SELECT count(*) as ukupan_broj, SUM(IF (sent_messages.delivered = 1,1,0 )) as broj_dostavljenih,
count(*) - SUM(IF (sent_messages.delivered = 1,1,0 )) as non_billed,
SUM(IF (sent_messages.delivered = 1,1,0 )) / count(*) as ratio,
`sent_messages`.`msisdn`,
MONTH(`sent_messages`.`datetime`) AS MONTH, WEEK(`sent_messages`.`datetime`) AS WEEK,
DATE_FORMAT(`sent_messages`.`datetime`, '%Y-%m-%d') AS DATE
FROM `sent_messages`
INNER JOIN `received_messages` on `received_messages`.`uniqueid`=`sent_messages`.`originalID`
and `received_messages`.`msisdn`=`sent_messages`.`msisdn`
WHERE `sent_messages`.`datetime` >= '2016-12-12'
AND `sent_messages`.`originalID` = `received_messages`.`uniqueid`
AND `sent_messages`.`datetime` <= '2017-12-30'
AND `sent_messages`.`datetime` >= `received_messages`.`datetime`
AND `sent_messages`.`datetime` <= ( `received_messages`.`datetime` + INTERVAL 2 HOUR )
AND `sent_messages`.`type` = 'PAID'
GROUP BY WEEK
ORDER BY DATE ASC
And because I'm grouping it by WEEK, my result is showing sum of all delivered, undelivered etc. but not per msisdn. Here is how result looks like:
And when I add msisdn in GROUP BY clause I don't get the result the way I need it.
And I need it like this:
Please help me to write optimized query to fetch these results for each msisdn per last 8 weeks, because I'm stuck.
WEEK(...) has a problem near the first of the year. Instead, you could use TO_DAYS:
WHERE datetime > CURDATE() - INTERVAL 8 WEEK -- for the last 8 weeks
GROUP BY MOD(TO_DAYS(datetime), 7) -- group by week
That is quite simple, but there is a bug in it. It only works if today is the last day of a "week". And if date%7 lands on the desired day of week.
WHERE datetime > CURDATE() - INTERVAL 9 WEEK -- for the last 8 weeks
GROUP BY MOD(TO_DAYS(datetime) - 3, 7) -- group by week
Is the first cut at fixing the bugs -- 9-week interval will include the current partial week and the partial week 8 weeks ago. The "- 3" (or whatever number works) will align your "week" to start on Monday or Sunday or whatever.
SUM(IF (sent_messages.delivered = 1,1,0 )) can be shortened to SUM(delivered = 1) or even SUM(delivered) if that column only has 0 or 1 values.
I need to do a select where I can chose to see results for current month, previous month, 1 month ago, 2 months ago, 3 months ago.
I found this question: MySQL: Query to get all rows from previous month, but I'm stuck with a filter that will get me all the results for 2 months ago from first to last day of the month.
I tried with this but it doesn't work:
SELECT * FROM table
AND MONTH(date_created) = MONTH(1 MONTH - INTERVAL 2 MONTH);
Try this:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(NOW() - INTERVAL 2 MONTH)
AND (
YEAR(date_created) = YEAR(NOW())
OR
YEAR(date_created) = YEAR(NOW() - INTERVAL 2 MONTH)
);
Returning records CREATED PRIOR the last 2 months only in MySQL.
If you want all rows from 2 months ago, then use logic like this:
WHERE date_created >= DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 2 MONTH) AND
date_created < DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 1 MONTH)
What is this doing? First, it is only applying functions to the current date part of the expression. This allows MySQL to use an index on date_created, if available and appropriate.
The expression DATE_SUB(CURDATE(), 1 - DAY(CURDATE()) is simply a way to get the first day of the month.
You query have an error, correct one would be:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(DATE_SUB(NOW(),INTERVAL 2 MONTH))
For current month just MONTH(NOW()), replace "2" with any number of months you need (1,3,.. 23)
as mentioned in comments this solution ignores YEAR differences, it just selects all records with the same month, no matter the year
you can filter wrong year results with additional clause:
AND YEAR(date_created) = '2019' # or year you need
Or use more complex query:
SELECT * FROM table
where
date_created between
/* first day of -2 month*/
date_sub(date_sub(now(),interval 2 month), interval (day(now())-1) day)
and
/* last day of -2 month*/
last_day(date_sub(now(),interval 2 month))
I am trying to find out how many times Kiln stopped in last three months. I have the following query:
SELECT SUM(kiln_no_stops) from monthly_report
where date BETWEEN DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%m-%Y')
AND DATE_FORMAT(NOW() , '%m-%Y')
When I use this query I get this error
Unknown column 'date' in 'where clause'.
But I am able to get the data for last one month using the query below:
SELECT SUM(kiln_no_stops)
from monthly_report
where date_format(yesterday,'%m-%Y')=Date_format(NOW() - INTERVAL 1 MONTH,'%m-%Y')
How can I get the data "Total number of stops" for the last three months?
Check if this would help you:
SELECT SUM(kiln_no_stops)
from monthly_report
where yesterday >= now()-interval 3 month;
EDIT:
To get last 3 months data (March1 to May31, current date = June 18)
SELECT SUM(kiln_no_stops)
from monthly_report where month(yesterday) < month(now())
AND yesterday >= cast( (last_day(now()) + interval 1 day - interval 4 month) as date);