The data is stored in DB in UTC , I have to convert local to UTC fetch it then show result in local time .
Now I am from India so if I want to search the data for today I have to query from 22nd March 6:30 PM UTC to 23rd March 6:30 PM UTC . Now Say I want to check data for every day of week until now which is 4:30 PM local . Now I wrote this query [ In simple words my objective is to get visitor number of each day from 12 AM to 4:30 PM ]
SELECT
FLOOR(TIMESTAMPDIFF(HOUR, "2017-03-16 18:29:59",
visit.date_created)/24) as dayofweek,
DAYOFWEEK(visit.date_created) day_num,
#rownum := #rownum + 1 as date_created_set_av,
count(distinct(visit.pkey)) AS Visits,
sum(revenue) AS Revenue,
sum(revenue) / count(distinct(visit.pkey)) as EPC
FROM la_20.visit, la_20.action
cross join (select #rownum := 0) r
WHERE visit.pkey=action.pkey and (visit.is_bot = 0)
AND visit.date_created >="2017-03-16 18:29:59"
AND visit.date_created <="2017-03-23 18:29:59"
AND TIME(visit.date_created)<="16:30:00"
GROUP BY dayofweek order by day_num
but what it does is fetch the value from 2017-03-23 00:00:00 to 2017-03-23 16:30:00 . What I need is to show result of 16th to 23rd everyday's data from 18:29:59 to 16:30:00. Remember I need the result of everyday's not one day's. Can anyone help
You should change your WHERE clause into
WHERE visit.pkey=action.pkey and (visit.is_bot = 0)
AND visit.date_created >="2017-03-22 18:29:59" and visit.date_created <="2017-03-23 14:29:59"
UPDATED
Today midnight: DATEADD(d,0,DATEDIFF(d,0,GETDATE()))
Today 4pm: DATEADD(HOUR, 16, DATEADD(d,0,DATEDIFF(d,0,GETDATE())))
WHERE visit.pkey=action.pkey and (visit.is_bot = 0)
AND visit.date_created >= DATEADD(d,0,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,0,DATEDIFF(d,0,GETDATE())))
Update 2:
Then you'll have to expand your where clause like this I guess.
WHERE visit.pkey=action.pkey and (visit.is_bot = 0)
AND (
(visit.date_created >= DATEADD(d, 0,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d, 0,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-1,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-1,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-2,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-2,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-3,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-3,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-4,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-4,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-5,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-5,DATEDIFF(d,0,GETDATE()))))
OR (visit.date_created >= DATEADD(d,-6,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-6,DATEDIFF(d,0,GETDATE()))))
)
Have you tried the following where clause instead?
WHERE visit.pkey=action.pkey and (visit.is_bot = 0)
AND visit.date_created >="2017-03-22 18:29:59"
AND visit.date_created <="2017-03-23 14:29:59"
3. Edit
OK, since OP wants something different again - and why not ;-) - here is another solution that might (or might not) solve his problem:
SELECT
COUNT( CASE WHEN d between
adddate(curdate(), interval -7 day) AND addtime(adddate(curdate(), interval -7 day),'16:00:00')
THEN 1 END ) dm7,
COUNT( CASE WHEN d between
adddate(curdate(), interval -6 day) AND addtime(adddate(curdate(), interval -6 day),'16:00:00')
THEN 1 END ) dm6,
COUNT( CASE WHEN d between
adddate(curdate(), interval -5 day) AND addtime(adddate(curdate(), interval -5 day),'16:00:00')
THEN 1 END ) dm5,
COUNT( CASE WHEN d between
adddate(curdate(), interval -4 day) AND addtime(adddate(curdate(), interval -4 day),'16:00:00')
THEN 1 END ) dm4,
COUNT( CASE WHEN d between
adddate(curdate(), interval -3 day) AND addtime(adddate(curdate(), interval -3 day),'16:00:00')
THEN 1 END ) dm3,
COUNT( CASE WHEN d between
adddate(curdate(), interval -2 day) AND addtime(adddate(curdate(), interval -2 day),'16:00:00')
THEN 1 END ) dm2,
COUNT( CASE WHEN d between
adddate(curdate(), interval -1 day) AND addtime(adddate(curdate(), interval -1 day),'16:00:00')
THEN 1 END ) dm1,
COUNT( CASE WHEN d between
curdate() AND addtime( curdate() ,'16:00:00')
THEN 1 END ) today
FROM (select '2017-03-17 15:00:01' d
union all select '2017-03-19 14:00:01'
union all select '2017-03-19 12:00:01'
union all select '2017-03-19 13:00:01'
union all select '2017-03-19 11:00:01'
union all select '2017-03-20 11:20:01'
union all select '2017-03-20 11:30:01'
union all select '2017-03-20 10:40:01'
union all select '2017-03-20 10:23:01' ) dates
This is an isolated query that works directly on the sample data given in the subquery. It results in the following list ("dm1"=day minus 1):
dm7 | dm6 | dm5 | dm4 | dm3 | dm2 | dm1 | today
1 | 0 | 4 | 4 | 0 | 0 | 0 | 0
You can test it here: http://rextester.com/ITLBL24540
Related
I'm trying to select between two dates like this:
SELECT p.Code, p.Name, sum(h.PA = 1) AS PA, sum(h.PB = 1) AS PB,
sum(h.PG = 1) AS PG, sum(h.GoedkeuringDoorNew = 'GF') AS GF,
sum(h.GoedkeuringDoorNew = 'SB') AS SB, sum(h.GoedkeuringDoorNew = 'VIA') AS VIA,
sum(h.Blanco) AS Blanco
FROM psthostess p
LEFT JOIN `psttodo-uit` h
ON h.`Hostess Code` = p.Code
AND DATE(h.`afgewerkt tablet datum`) BETWEEN CURDATE()
AND (CURDATE() - INTERVAL 1 DAY)
WHERE p.Indienst = 1 GROUP BY p.Code, p.Name
But I don't get any results.
I want the results of today and yesterday. But when I do this
SELECT *
FROM `psttodo-uit` p
WHERE DATE(p.`afgewerkt tablet datum`) = CURDATE() - INTERVAL 1 DAY
or change CURDATE() - INTERVAL 1 DAY into CURDATE() I get results... How does this come?
WHERE p.`afgewerkt tablet datum` >= CURDATE() - INTERVAL 1 DAY
AND p.`afgewerkt tablet datum` < CURDATE() + INTERVAL 1 DAY
Assuming current date and time is: April 11, 2014 05:30PM, the datetime you need is betweenApril 10, 2014 00:00:00 and April 11, 2014 23:59:59 (inclusive)
CURDATE() - INTERVAL 1 DAY => April 10, 2014 00:00:00
CURDATE() + INTERVAL 1 DAY => April 12, 2014 00:00:00
Select * from emp where joindate between SUBDATE(NOW(),1) and NOW();
I have two similar mysql tables and want to get the data from both of them in one output (merge). How can I do it?
For this reason I have created two separate queries to check that I get what I'm looking for.
First: Table -> web_session
SELECT date_format(booking_time, '%m-%Y') AS m, count(booking_time) AS b
FROM web_session
WHERE
date(booking_time) >= date_sub(curdate(), interval 12 month)
AND
date(booking_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(booking_time)
ORDER BY booking_time ASC;
Output:
m, b
10-2013, 15
11-2013, 6
12-2013, 13
01-2014, 10
02-2014, 10
03-2014, 25
04-2014, 1
Second: Table -> web_log
SELECT date_format(request_time, '%m-%Y') AS m, count(request_time) AS r
FROM web_log
WHERE
date(request_time) >= date_sub(curdate(), interval 12 month)
AND
date(request_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(request_time)
ORDER BY request_time ASC;
Output:
m, r
03-2014, 45
04-2014, 35
desired output:
m, b, r
10-2013, 15, null
11-2013, 6, null
12-2013, 13, null
01-2014, 10, null
02-2014, 10, null
03-2014, 25, 45
04-2014, 1, 35
You need to join them. Try this:
SELECT T1.m,T1.b,T2.r FROM
(
SELECT date_format(booking_time, '%m-%Y') AS m, count(booking_time) AS b
FROM web_session
WHERE
date(booking_time) >= date_sub(curdate(), interval 12 month)
AND
date(booking_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(booking_time)
ORDER BY booking_time ASC;) T1
LEFT OUTER JOIN
(SELECT date_format(request_time, '%m-%Y') AS m, count(request_time) AS r
FROM web_log
WHERE
date(request_time) >= date_sub(curdate(), interval 12 month)
AND
date(request_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(request_time)
ORDER BY request_time ASC;) T2
ON T1.m=T2.m
ORDER BY T1.m
Output will be:
m b r
10-2013 15 null
11-2013 6 null
12-2013 13 null
01-2014 10 null
02-2014 10 null
03-2014 25 45
04-2014 1 35
All date format in European (yyyy-mm-dd).
I am trying find the corresponding start date for a week reported by the weekofyear(this is, week starting on Monday).
set #date0 ='2012-12-31';
set #date1 ='2013-01-01';
select weekofyear(#date0), weekofyear(#date1);
> 1, 1
SQL Fidle 1
This is, both dates are in the first week of 2013.
However, if I try to extract the year and weekofyear I will get different results (which means I need to find a different strategy):
set #date0 ='2012-12-31';
select year(#date0), weekofyear(#date0);
>2012,1
set #date1 ='2013-01-01';
select year(#date1), weekofyear(#date1);
>2013,1
SQl Fidle 2
If I manually consult the calender I can see to which year they belong (2013).
this big case will output the week start date
set #date1 ='2012-01-01';
select
case when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 1 day)) then #date1
when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 2 day)) then date_sub(#date1 , interval 1 day)
when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 3 day)) then date_sub(#date1 , interval 2 day)
when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 4 day)) then date_sub(#date1 , interval 3 day)
when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 5 day)) then date_sub(#date1 , interval 4 day)
when weekofyear(#date1) <> weekofyear(date_sub(#date1 , interval 6 day)) then date_sub(#date1 , interval 6 day)
else date_sub(#date1 , interval 6 day) end as week_start_date;
>2011-12-26
SQL Fidle 3
and this big case will also generate the combo year-week
set #date1 ='2012-12-31';
select
case when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 1 day)) then concat(year(#date1),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 2 day)) then concat(year(date_add(#date1 , interval 1 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 3 day)) then concat(year(date_add(#date1 , interval 2 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 4 day)) then concat(year(date_add(#date1 , interval 3 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 5 day)) then concat(year(date_add(#date1 , interval 4 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
when weekofyear(#date1) <> weekofyear(date_add(#date1 , interval 6 day)) then concat(year(date_add(#date1 , interval 5 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1))
else concat(year(date_add(#date1 , interval 6 day)),'-', if(weekofyear(#date1) < 10, '0','') ,weekofyear(#date1)) end as week_of_year;
> 2013-01
SQL Fidle 4
Now i either need to find an more elegant way of do it, or find a suitable strategy to include this in the group by condition. I was thinking in add an extra column to the table being grouped and -- after add an index --, group by week_of_year or by the week_start_date.
Does someone experienced have a better idea/strategy?
Notes: this is to be used in a database with over half million users, to Analise a certain action they perform and the group by condition will take other parameters (such as, but not limited to, demographics).
I had tried this code:
Its works also fine,but the issue is, if current month is feb and fire this query then it considers past 3 months from now and hence starts from past year i.e 2012 nov or dec say i want only current year data,if it is feb now and i fire this query then it should only show jan and feb records.
SELECT CROEmailId,
(
SELECT COUNT(LeadId)
FROM LeadStatus
WHERE DATE(`LeadTime`)> DATE_SUB(now(),
INTERVAL 3 MONTH
)
AND Generated=1 and AssignedTo=a.CROEmailId)
AS 'NEW LEAD',(
SELECT COUNT(LeadId)
FROM LeadHistory
WHERE DATE(UpdatedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH
) AND AssignedTo=a.CROEmailId)
AS 'Lead Updated',
(
SELECT SUM(TotalEmails)
FROM MailJobs
WHERE DATE(CompletedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH
)
AND MailFrom=a.CROEmailId)
AS 'Email Uploaded',
(
SELECT SUM(TotalSent)
FROM MailJobs
WHERE DATE(CompletedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId
)
AS 'Email Sent',
(
SELECT SUM(NetTotal)
FROM Invoice
WHERE Status='PAID'
AND DATE(CreatedAt)> DATE_SUB(now(), INTERVAL 3 MONTH)
AND CROEmailId=a.CROEmailId)
AS 'Payment Today' FROM CustomersManager a;
Try change
DATE_SUB(now(), INTERVAL 3 MONTH)
to
IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
in all subqueries.
SELECT CROEmailId,
(SELECT COUNT(LeadId)
FROM LeadStatus
WHERE DATE(`LeadTime`)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND Generated=1
AND AssignedTo=a.CROEmailId) AS 'NEW LEAD',
(SELECT COUNT(LeadId)
FROM LeadHistory
WHERE DATE(UpdatedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND AssignedTo=a.CROEmailId) AS 'Lead Updated',
(SELECT SUM(TotalEmails)
from MailJobs
WHERE DATE(CompletedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId) AS 'Email Uploaded',
(SELECT SUM(TotalSent)
FROM MailJobs
WHERE DATE(CompletedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId) AS 'Email Sent',
(SELECT SUM(NetTotal)
FROM Invoice
WHERE Status='PAID'
AND DATE(CreatedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND CROEmailId=a.CROEmailId) AS 'Payment Today'
FROM CustomersManager a;
use this in your query to find record filter by year
YEAR( '20013-12-12' )
example,
SELECT * FROM TABLE WHERE YEAR(DATE_FIELD) = 2013
Suppose I have a table that contain information on streaming media connections. In this table, I have a start time and end time for when the connection was initiated and then later closed.
Table: logs
id (INT, PK, AUTO_INCREMENT)
StartTime (DATETIME)
EndTime (DATETIME)
I want to be able to run a query that will add up the total time connections were established for a day. This is obvious for connections within a day:
SELECT
SUM(
TIME_TO_SEC(
TIMEDIFF(`EndTime`, `StartTime`)
)
)
WHERE (`StartTime` BETWEEN '2010-01-01' AND '2010-01-02);
However, suppose a StartTime begins one day, say around 11:00PM, and EndTime is some time the next day, maybe 3:00AM. In these situations, I want to allocate only the amount of time that occurred during the day, to that day. So, 1 hour would go towards the first day, and 3 hours would go to the next.
SUM(
TIME_TO_SEC(
TIMEDIFF(
IF(`EndTime`>DATE_ADD('2010-01-01', INTERVAL 1 DAY), DATE_ADD('2010-01-01', INTERVAL 1 DAY), `EndTime`),
IF(`StartTime`<'2010-01-01', '2010-01-01', `StartTime`)
)
)/60/60
)
The thinking with this is that if the EndTime is more than the end of the day, then we'll just use the end of the day instead. If the StartTime is less than the beginning of the day, then we'll just use the beginning of the day instead.
So, I then need to wrap this all up into something that will generate a table that looks like this:
date, total
2010-01-01, 0
2010-01-02, 1.53
2010-01-03, 5.33
I thought this query would work:
SELECT
`date`,
SUM(
TIME_TO_SEC(
TIMEDIFF(
IF(`EndTime`>DATE_ADD(`date`, INTERVAL 1 DAY), DATE_ADD(`date`, INTERVAL 1 DAY), `EndTime`),
IF(`StartTime`<`date`, `date`, `StartTime`)
)
)/60/60
) AS `total_hours`
FROM
(SELECT * FROM `logs` WHERE `StartTime` BETWEEN '2010-08-01' AND '2010-08-31') AS logs_small,
(SELECT DATE_ADD("2010-08-01", INTERVAL `number` DAY) AS `date` FROM `numbers` WHERE `number` BETWEEN 0 AND 30) AS `dates`
GROUP BY `date`;
Note the numbers table referenced is a table with just one column, number, with a series of integers, 0, 1, 2, 3, etc. I am using it here to generate a series of dates, which works fine.
The problem with this query is that I get inaccurate data. Specifically, rows in the logs table that have an EndDate that goes into the next day don't get any time counted in that next day. For example, if I had a row that started 2010-08-01 23:00:00 and ended 2010-08-02 01:00:00, then the resulting row for 2010-08-02 would add up to 0.
Is there a better way to do this? Ideally, I'd like to get 0 instead of null on days that don't have any records that match up to them as well.
Edit: To clarify, I want to turn this:
id, StartTime, EndTime
0, 2000-01-01 01:00:00, 2000-01-01 04:00:00
1, 2000-01-01 23:00:00, 2000-01-02 05:00:00
2, 2000-01-02 00:00:00, 2000-01-04 01:00:00
... into this:
date, total_hours
2000-01-01, 4
2000-01-02, 29
2000-01-03, 24
2000-01-04, 1
2000-01-05, 0
Solution
Thanks to jim31415 for coming up with the solution! I translated his answer over to the functions usable in MySQL and came up with this:
SELECT `d`.`Date`,
SUM(COALESCE(
(CASE WHEN t.StartTime >= d.Date AND t.EndTime < DATE_ADD(d.Date, INTERVAL 1 DAY) THEN TIME_TO_SEC(TIMEDIFF(t.EndTime, t.StartTime))
WHEN t.StartTime < d.Date AND t.EndTime <= DATE_ADD(d.Date, INTERVAL 1 DAY) THEN TIME_TO_SEC(TIMEDIFF(t.EndTime,d.Date))
WHEN t.StartTime >= d.Date AND t.EndTime > DATE_ADD(d.Date, INTERVAL 1 DAY) THEN TIME_TO_SEC(TIMEDIFF(DATE_ADD(d.Date, INTERVAL 1 DAY),t.StartTime))
WHEN t.StartTime < d.Date AND t.EndTime > DATE_ADD(d.Date, INTERVAL 1 DAY) THEN 24*60*60
END), 0)
)/60/60 ConnectionTime
FROM (SELECT DATE_ADD('2011-03-01', INTERVAL `number` DAY) AS `Date` FROM `numbers` WHERE `number` BETWEEN 0 AND 30) AS d
LEFT JOIN `logs` t ON (t.StartTime >= d.Date AND t.StartTime < DATE_ADD(d.Date, INTERVAL 1 DAY))
OR (t.EndTime >= d.Date AND t.EndTime < DATE_ADD(d.Date, INTERVAL 1 DAY))
OR (t.StartTime < d.Date AND t.EndTime > DATE_ADD(d.Date, INTERVAL 1 DAY))
GROUP BY d.Date
ORDER BY d.Date;
I should also note that the null values for EndTime weren't applicable in my situation, as I am reading from old log files in my application. If you need them though, Jim's post has them outlined quite well.
This is in MS SQL, but I think the logic applies and can be translated into MySQL.
I wasn't sure how you wanted to handle EndTime that are null, so I commented that out.
select d.Date,
sum(coalesce(
(case when t.StartTime >= d.Date and t.EndTime < dateadd(day,1,d.Date) then datediff(minute,t.StartTime,t.EndTime)
when t.StartTime < d.Date and t.EndTime <= dateadd(day,1,d.Date) then datediff(minute,d.Date,t.EndTime)
when t.StartTime >= d.Date and t.EndTime > dateadd(day,1,d.Date) then datediff(minute,t.StartTime,dateadd(day,1,d.Date))
when t.StartTime < d.Date and t.EndTime > dateadd(day,1,d.Date) then 24*60
--when t.StartTime >= d.Date and t.EndTime is null then datediff(minute,t.StartTime,getdate())
--when t.StartTime < d.Date and t.EndTime is null then datediff(minute,d.Date,getdate())
end), 0)
) ConnectionTime
from (select Date=dateadd(day, num, '2011-03-01') from #NUMBERS where num between 0 and 30) d
left join Logs t on (t.StartTime >= d.Date and t.StartTime < dateadd(day,1,d.Date))
or (t.EndTime >= d.Date and t.EndTime < dateadd(day,1,d.Date))
or (t.StartTime < d.Date and t.EndTime > dateadd(day,1,d.Date))
group by d.Date
order by d.Date
Use a union to make it easier for yourself
SELECT
`date`,
SUM(
TIME_TO_SEC(TIMEDIFF(`EndTime`,`StartTime`))/60/60
) AS `total_hours`
FROM
(SELECT id, starttime, if (endtime > date then date else endtime) FROM `logs` WHERE `StartTime` >= date AND `StartTime` < date
union all
SELECT id, date, endtime FROM `logs` WHERE `enddate` >= date AND `enddate` < date and !(`StartTime` >= date AND `StartTime` < date)
union all
SELECT id, date, date_add(date, 1) FROM `logs` WHERE `enddate` > date AND `startdate` < date
) as datedetails inner join
(SELECT DATE_ADD("2010-08-01", INTERVAL `number` DAY) AS `date` FROM `numbers` WHERE `number` BETWEEN 0 AND 30) AS `dates`
GROUP BY `date`;
Hope, I understood your question correctly
Edit: Forgot case when there is a multiday request that starts before the day asked for, and ended after
Use this
select startTime,duration as duration,time,TIME_TO_SEC(TIMEDIFF(time,startTime)) as diff from <idling> limit 25;
select startTime,duration DIV 60 as duration,time,TIMESTAMPDIFF(MINUTE,startTime,time) as diff from <idling> limit 25;