Group rows by 7 days interval starting from a certain date - php

Is there a way to group rows by a 7 days intervals(datetime) starting from a certain date in Mysql?

SELECT
1 + DATEDIFF(columnDate, #start_date) DIV 7 AS weekNumber
, #start_date + INTERVAL (DATEDIFF(columnDate, #start_date) DIV 7) WEEK
AS week_start_date
, MIN(columnDate) AS actual_first_date
, MAX(columnDate) AS actual_last_date
, SUM(otherColumn)
, AVG(otherColumn)
---
FROM
tableX
WHERE
columnDate >= #start_date
GROUP BY
DATEDIFF(columnDate, #start_date) DIV 7 ;

SELECT *
FROM `table`
GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) )

SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
This selects the users created between 201-12-01 and 7 days after that.
Make changes, to query based on your need.

Related

SQL - Sum last value by date of current week

I need to do a SUM of the last value (by date) of each day of the current week. To test I have a table with the values:
5 for 2023-01-12 16:53:01
2 for 2023-01-12 08:49:03
5 for 2023-01-11 08:58:19
I expect a result of 10.
I'm trying with the following code:
SELECT SUM(value) AS weeklyValue
FROM (
SELECT value
FROM table
WHERE WEEK(DATE(DataOra)) = WEEK(NOW())
AND WEEKDAY(DATE(DataOra)) >= 1
AND WEEKDAY(DATE(DataOra)) <= 7
AND DataOra = (SELECT MAX(DataOra) FROM table WHERE WEEKDAY(DataOra) = WEEKDAY(DATE(DataOra)) )
GROUP BY WEEKDAY(DATE(DataOra))
) AS subquery;
but the result is 5. Where is the mistake? Thanks
Maybe I found a solution:
SELECT SUM(Value) AS energiaSettimanale
FROM (
SELECT MAX(Value) as value, WEEKDAY(DataOra) as d
FROM table
WHERE WEEK(DATE(DataOra)) = WEEK(NOW())
AND WEEKDAY(DATE(DataOra)) >= 0
AND WEEKDAY(DATE(DataOra)) <= 6
GROUP BY d
) AS subquery;
If I really understood what you want, this request may be (if I understood correctly, I am specifying) the solution
select sum(value) from `table`
inner join (
select max(DataOra) as maxDataOra
from `table`
where WEEK(DATE(DataOra))=WEEK(NOW())
group by WEEKDAY(DataOra)
)
as tmp on DataOra=tmp.maxDataOra
The trick to find the most recent date of the day being to group by day number with the max() function on dateTime Data.
You can do it using inner join as follows :
select weekNumber, sum(value) from (
select t.value, week(s.maxDataOra) as weekNumber
from _table t
inner join (
select MAX(DataOra) as maxDataOra
from _table
group by DATE(DataOra)
) as s on s.maxDataOra = t.DataOra
) as b
group by weekNumber;
Check it here : https://dbfiddle.uk/hadzywwh
I need to do a SUM of the last value (by date) of each day of the current week.
So, let's start by constraining our working set to just the current week's data -
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
Then we can use that to constrain the query used to get the MAX(DataOra) per DATE(DataOra) and join back to the original dataset to get the sum of the respective Values -
SELECT SUM(`Value`) `energiaSettimanale`
FROM `table` `t1`
INNER JOIN (
SELECT MAX(`DataOra`) AS `maxDataOra`
FROM `table`
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
GROUP BY DATE(`DataOra`)
) `t2` ON `t2`.`maxDataOra` = `t1`.`DataOra`;
Or if using MySQL >= 8.0 you could use the ROW_NUMBER() window function -
SELECT SUM(`Value`) `energiaSettimanale`
FROM (
SELECT `Value`, ROW_NUMBER() OVER (PARTITION BY DATE(`DataOra`) ORDER BY `DataOra` DESC) `rn`
FROM `table`
WHERE `DataOra` >= (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY) /* first day of current week (Mon - Sun) */
AND `DataOra` < (CURRENT_DATE - INTERVAL(WEEKDAY(CURRENT_DATE)) DAY + INTERVAL 1 WEEK) /* first day of next week (Mon - Sun) */
) `t`
WHERE `rn` = 1;

How to edit this query to get last 7 days visits even if there are no visits on specific days? [duplicate]

This question already has answers here:
MySQL how to fill missing dates in range?
(6 answers)
MySQL: Select All Dates In a Range Even If No Records Present
(6 answers)
Closed 2 years ago.
I have the following query that returns the dates first_visit starting from today and 7 days back, as well as the visitors hash per day:
SET time_zone= '{$company_timezone}';
SELECT DATE( first_visit ) AS day , COUNT( DISTINCT hash ) AS total
FROM table
WHERE company = 1 and first_visit > SUBDATE( CURDATE( ) , 7 )
GROUP BY day
The flaw with this is that if company = 1 have visitors only today and three days ago, I will get this:
day --------- total
2020-03-08 ----- 30
2020-03-05 ----- 40
leaving out all other dates inbetween.
What I want is to get all the past 7 days, even there are no visitors at all. If there are no visitors, then it should just show 0.
How to edit my query in order to achieve this?
Thank you
Perform an outer join with a derived table that contains desired dates:
select b.date as day, count(distinct hash) as total
from table
right join (select #now := #now - interval 1 day as date from (select #now := curdate()) a, table limit 7) b
on b.date = date(first_visit) and company = 1
group by b.date
This assumes that table has at least 7 rows.
Note: there are two occurrences of table.
If you have data for each day -- but not for that company -- then conditional aggregation is a pretty simply approach:
SELECT DATE( first_visit ) AS day ,
COUNT( DISTINCT CASE WHEN company = 1 THEN hash END ) AS total
FROM table
WHERE first_visit > SUBDATE( CURDATE( ) , 7 )
GROUP BY day;
This only works if all days are represented in your table for some company.
Some solutions involve a table of numbers.
Here is one way with a recursive query, available in MySQL 8.0:
with d as (select 0 n union all select n + 1 where n < 6)
select
current_date - interval n day myday,
count(distinct t.hash) total
from d
left join mytable t
on t.company = 1
and t.first_visit >= current_date - inteval n day
and t.first_visit < current_date - interval (n - 1) day
group by d.n
In earlier version, you can enumerate the numbers as a derived table:
select
current_date - interval n day myday,
count(distinct t.hash) total
from (
select 0 n union all select 1 union all select 2 union all select 3
union all select 4 union all select 5 union all select 6 union all select 7
) d
left join mytable t
on t.compatny = 1
and t.first_visit >= current_date - inteval n day
and t.first_visit < current_date - interval (n - 1) day
group by d.n

how to take the count based on first week and second and third week and fourth week

I have one table name (task), here i want take the count like how many registration happend in first and how many registration happend in second how how can do this , i am the new persion of mysql and PHP .
id name t_created_on
1 Kani 2017-03-03 12:45:18
2 yuvi 2017-03-04 12:45:18
3 Mahesh 2017-03-11 12:45:18
Here i am using this format date("Y-m-d h:i:s")
Bassed on my database first week 2 registration and second week is 1.
Expected results:
First Week count : 2
Second week count : 1
1.
SELECT count(*) as records FROM tbl
WHERE t_created_on >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND t_created_on < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
2.
SELECT count(*) as weekrecord FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1 AND YEAR( date) = YEAR( current_date );
note: WEEK( current_date ) -1: number indicates number of week.
SELECT WEEK(t_created_on) as week, count(*) as count
FROM my_table
GROUP BY WEEK(t_created_on)
ORDER BY WEEK(t_created_on);
SELECT count(*) as weekrecord, MONTH(current_date) as monthwise
FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1
AND YEAR( date) = YEAR( current_date );

MySQL select all days in date range even if no data is available for day

I have entries for each days as a punch in/out clock. I would like to display all days for the date givens week and show the days even if no data in found for that day.
Example:
Monday 2015-05-04
- 2015-05-04 10:30:00
- 2015-05-04 15:45:34
Tuesday 2015-05-05
- 2015-05-05 08:43:23
- 2015-05-05 17:18:13
Wednesday 2015-05-06
- 2015-05-06 09:03:12
Thursday 2015-05-07
0 Entries
Friday 2015-05-08
0 Entries
The DB schema looks like:
id | user_id | punch_time | punch_status
I would ideally like to be able to change the date given to MySQL to any timestamp and it would show the days and results for that week.
Thanks :D
NEW
Any ideas why this doesn't work to get days when no records are present?
SELECT * FROM punch_clock, calendar_table WHERE calendar_table.dt = DATE(punch_clock.punch_time) && calendar_table.week_num = $week && calendar_table.y = $year ORDER BY punch_clock.punch_time
Newer Query
SELECT * FROM punch_clock LEFT JOIN calendar_table ON calendar_table.dt = DATE(punch_clock.punch_time) WHERE calendar_table.week_num = 18 && calendar_table.y = 2015;
With MySQL, I usually use a calendar table for this purpose (containing all the dates until 2030 for example)
It allows to do many other things like this kind of queries, manage special days, etc..
You'll want to LEFT JOIN your table on it, I mean this calendar table have to be "left positioned"
Taking your last query, I'd do this:
SELECT *
FROM calendar AS cal
LEFT JOIN punch_clock AS puc
ON (cal.dt = DATE(puc.punch_time))
WHERE TRUE
AND cal.week_num = 18
AND cal.y = 2015
;
Didn't try, but this is the idea.
Have a try of this:
SELECT *
FROM
(
SELECT a.Date AS mydate
FROM (
SELECT date('2015-05-08') - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date
FROM (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS c
) a
WHERE a.Date BETWEEN '2015-05-04' AND '2015-05-08'
) dates
LEFT JOIN
(
SELECT *
FROM
table1
) data
ON DATE_FORMAT(dates.mydate, '%Y%m%d') = DATE_FORMAT(data.punch_time, '%Y%m%d')
SQL Fiddle: http://sqlfiddle.com/#!9/72ee3/15/0
This is a fast but not that ideal solution for your question. But I think it's just enough for use.
If you'd like to solve the problem "perfectly", I suggest you reading this article: http://www.brianshowalter.com/calendar_tables
Even while this is a really old question I did not like the idea of an extra calendar table and came up with this query, adjusted for the question.
SELECT pc.`id`, pc.`user_id`, pc.`punch_status`, dates.`date` FROM punch_clock pc RIGHT JOIN (SELECT week.`date` FROM (
SELECT CURDATE() AS `date`
UNION SELECT CURDATE() + INTERVAL 1 DAY
UNION SELECT CURDATE() + INTERVAL 2 DAY
UNION SELECT CURDATE() + INTERVAL 3 DAY
UNION SELECT CURDATE() + INTERVAL 4 DAY
UNION SELECT CURDATE() + INTERVAL 5 DAY
UNION SELECT CURDATE() + INTERVAL 6 DAY
UNION SELECT CURDATE() - INTERVAL 1 DAY
UNION SELECT CURDATE() - INTERVAL 2 DAY
UNION SELECT CURDATE() - INTERVAL 3 DAY
UNION SELECT CURDATE() - INTERVAL 4 DAY
UNION SELECT CURDATE() - INTERVAL 5 DAY
UNION SELECT CURDATE() - INTERVAL 6 DAY
) AS week
WHERE YEARWEEK(`date`, 0) = YEARWEEK(CURDATE(), 0)) AS dates ON dates.`date` = DATE(pc.`punch_time`) ORDER BY dates.`date`
Not exactly pretty, but it does the job without an extra table. This uses a week from Monday to Sunday, use YEARWEEK(xxx, 1) for Sunday to Monday week.

Single query to get count of individual of past 7 days

I do know that the following conditions will return the total number in the past 7 days
SELECT count(id) FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
Is there a single query that I can do to get the past 7 days returning in an array of 7 results of each individual day?
For example:
day 1 - 10
day 2 - 5
day 3 - 9
..
..
..
This will give you the date and the count.
SELECT DATE(createdDate),COUNT(id)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
Alternatively to give a result closer to your example you could use:
SELECT CONCAT("Day ",DATEDIFF(NOW(), createdDate)) AS day,COUNT(id)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
Add a group-by clause:
SELECT count(id), DATE(createdDate)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
select 8 - n day, count(id)
from registration
join (select 1 n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7) x
on createdDate between date_sub(curdate(), interval n day) and date_sub(curdate(), interval n-1 day)
group by day
order by day

Categories