Report For Every Day But Skipping 2 hours - mysql

I am using the following query to generate employee report for every day
Employee Report For Yesterday (12:00 to 12:00)
SELECT * FROM emp WHERE DATE(created) = DATE(NOW() - INTERVAL 1 DAY);
 
Employee Report For 24 Hours from Current Time When Job is running
 
SELECT * FROM emp WHERE (created > DATE_SUB(NOW(), INTERVAL 1 DAY));
In both the reports i want to skip records from 10:00 am. to 11:00 a.m and 4:00 p.m to 5:00 p.m records on that day
Please help me on how to do it?
Thanks

It can look like
SELECT *
FROM emp
WHERE DATE(created) = CURRENT_DATE - INTERVAL 1 DAY
AND TIME(created) NOT BETWEEN '10:00' AND '11:00'
AND TIME(created) NOT BETWEEN '16:00' AND '17:00';

Related

Current time but for yesterday

Trying to compare today's current revenue total to yesterdays revenue total where yesterday revenue total is only calculated for the same hours that were calculated for today.
Ex:-
Today: 12 am to 7 pm
Yesterday: 12 am to 7 pm
I want to make sure that yesterday data is always in sync with the current time of today.
Here's what I've tried for pulling yesterday data.
Table Columns:-
date : date ex. 2019-07-08
hour : hour ex. 2019-07-08 23:00:00 (in hour increments)
revenue : revenue
SELECT
sum(revenue) yesterday_revenue
from hour
where hour <= CURRENT_TIME - INTERVAL 24 hour and date >= CURRENT_DATE - INTERVAL 1 day
group by 1
I think this is what you want:
select sum(revenue) as yesterday_revenue
from hour
where hour <= curtime()
date = curdate() - interval 1 day
You can try this below query to get the record as you want.
SELECT
sum(revenue) as yesterday_revenue
from hour
where hour <= DATE_SUB(NOW(), INTERVAL 24 HOUR) and date >= (curdate() - interval 1 day)
group by 1

How to show current months projects in list, where current month date is between A and B on new year roll over

Last year I set up a project management system, but now as we're on a new year I've been bit in my ass by some newbie coding (of me) :)
So now I really need your help to understand how to fix this in a good way once and for all.
The error:
The major problem right now is in the year roll over, right now my SQL query dosn't understand that the month of the new year (1) is larger than the current years (12) which then dosn't show the right projects in the list.
Any ideas? Thanks in advance!
This is my current SQL Query:
SELECT *
FROM projects
WHERE MONTH(CURDATE()) between MONTH(project_start) and MONTH(project_delivery)
AND YEAR(CURDATE()) between YEAR(project_start) and YEAR(project_delivery)
order by project_id
This is the raw structure of the table projects:
Project_id projet_start projet_delivery
1 2018-12-20 2018-12-22
2 2018-12-25 2018-12-29
3 2018-12-28 2018-12-28
4 2018-12-30 2019-01-22
BETWEEN works for actual dates:
SELECT *
FROM projects
WHERE CURDATE() between project_start and project_delivery
order by project_id
If you wanted everything from the month (month granularity) rather than day granularity:
SELECT *
FROM projects
WHERE CURDATE() between DATE_SUB(project_start, INTERVAL DAY(project_start)-1 DAY) and
DATE_ADD(DATE_SUB(project_delivery, INTERVAL DAY(project_delivery) DAY), INTERVAL 1 MONTH)
order by project_id
So if a project started on Dec 5th and finished on Jan 19th, this would give everything between Dec 1st and Jan 31st. Do bear in mind though that if any date on Jan 31st also has a time component (i.e. later than midnight), it will mean it's fractionally after this end date and won't show.
Comment if that's the case and you want help solving it (easiest to not use BETWEEN because it's always inclusive at each end, use < which is exclusive)
Update: Projects that have had some activity this month, i.e a project that:
started before, ended during
started before, ended after
started during, ended during
started during, ended after
The common thing all these have is that the start date of the project is before the end of this month, and also the end date of the project is after the first of this month
SELECT *
FROM projects
WHERE
--started before the end of this month
project_start < DATE_ADD(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) - 1 DAY), INTERVAL 1 MONTH) AND
--ended after the start of this month
project_delivery > DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) DAY)
order by project_id
Doing a DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) DAY) is a rather convoluted way of writing "subtract the current day number from the current date" i.e. 2019-01-02 minus 2 -> 2018-12-31. We look for dates > this (so as not to include it)
Similarly, DATE_ADD(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) - 1 DAY), INTERVAL 1 MONTH) Takes the current date, subs DAY-1 (so just 1 this time, not 2) off it to reach 1st of this month, then adds a month on to get to first of next month. 2019-01-02 -> 2019-01-01 -> 2019-02-01. Again < it so it's exclusive
There's a bug in going the other way (add a month then sub the day) if you're on eg the 31st of jan and you add a month first - there is no 31st of feb so mysql will cap at 28th of feb, then sub 31 days off it, giving a date that is not the end of jan (i.e. 28th of jan)
Just use date comparisons:
select p.*
from projects p
where curdate() >= project_start and
curdate() <= project_delivery;
I'm not sure why you would want to break the dates into their time components. However, that is totally unnecessary to compare them.
If you want to just do the comparison at the month level, then one method is to convert the values to months:
select p.*
from projects p
where year(curdate()) * 12 + month(curdate()) >= year(project_start) * 12 + month(project_start) and
year(curdate()) * 12 + month(curdate()) <= year(project_delivery) * 12 + month(project_delivery);
Alternatively, just move the dates to the beginning of months:
select p.*
from projects p
where curdate() >= project_start + interval 1 - day(project_start) day and
curdate() < ( project_delivery + interval (1 - day(project_delivery) day) + interval 1 month;

MySQL select from a period of time

I'd like to build a query that updates daily at 1pm, which shows static data from 3pm to 11am nexy day.
if now is 2018-06-16 12pm, shows data from 2018-06-15 3pm - 2018-06-16 11am
if now is 2018-06-16 1pm, shows data from 2018-06-16 3pm - 2018-06-17 11am
Here is the query I tried.
UPDATED:
SELECT * FROM config WHERE gt BETWEEN CONCAT(curdate(),' 15:00:00') AND CONCAT(curdate()+ INTERVAL 1 DAY,' 10:59:59')
ISSUES:
When today is 2018-06-15, curdate() -> 2018-06-15, which shows data of today which no issues.
When today is 2018-06-16, curdate() -> 2018-06-16, which shows data of next day way too early, what i want is update next day 1pm daily.
Two cases:
It's between 0:00 and 12:59: you want yesterday 15:00 till today 11:00
It's between 13:00 and 23:59: you want today 15:00 till tomorrow 11:00
The query:
select *
from config
where
(
hour(current_time) between 0 and 12
and gt >= current_date - interval 9 hours -- yesterday 15:00
and gt < current_date + interval 11 hours -- today 11:00
)
or
(
hour(current_time) between 13 and 23
and gt >= current_date + interval 15 hours -- today 15:00
and gt < current_date + interval 35 hours -- tomorrow 11:00
);
lot of ways you can apply select command. here I mentioned the only 2 ways. this is useful for you.
SELECT * FROM config
WHERE gt BETWEEN #07/04/1996# AND #07/09/1996#;
select * from config where gt and created_at > (now()- interval 10 day)
You can use this sample to build your query:
SELECT *
FROM table_name
WHERE `date` >= NOW() - INTERVAL 10 DAY;
Here some information on how to work with INTERVAL.
And here is some useful information how to execute query at a particular time.
EDIT:
if now() = 2018-06-16 1pm, shows data from 2018-06-16 3pm - 2018-06-17
11am
Then you ca use this example:
SELECT *
FROM config
WHERE DATE(gt) BETWEEN DATE(NOW() + INTERVAL 2 DAY_HOUR) AND -- 2018-06-16 3pm
DATE(NOW() + INTERVAL 22 DAY_HOUR); -- 2018-06-17 11am
DEMO here
Note: the first DATE should be earlier as second DATE in BETWEEN statement. Otherwise it won't work.

add days to a date column and then check if it's between two dates SQL

I need a sql query to find the data from a employee table.
Startdate is present in employee table.
I need to add 90 days in startdate and then I need to check if the startdate lies in the current month or not.
I did try below query:
SELECT *
FROM `employees`
WHERE DATE_ADD(str_to_date(startdate, '%m/%d/%Y'),INTERVAL 90 DAY) BETWEEN '09/01/2016' AND '09/30/2016'
but its not giving me the expected results.(I do have data which should show up if the query is correct.)
Hi i did change the query and ran, please see the result. I am getting results of next month too :(
This is the query
SELECT id,startdate from employees WHERE str_to_date(startdate, '%m/%d/%Y') between DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day)
Query output
Rephrased my query...
Where the event was within the dates 90 days before the start and end of this month
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day) -- End of this month - 90 days
or, for three months
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 3 month) -- start of this month - 3 months
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 3 month) -- End of this month - 3 months

Mysql not between time and dates

user | completed
mike | 2016-07-10 19:00:00
john | 2016-07-11 08:00:00
I am trying to select all rows in a database where the row completed is NOT between 14:00 the previous day and the current day before 10:00. The script is designed to be run at 10:30 everyday
I've tried this
SELECT name FROM daily_tracking WHERE completed NOT BETWEEN now() - interval 1 day AND NOW() - INTERVAL 8 hour
you should use date_sub
SELECT name
FROM daily_tracking
WHERE completed NOT BETWEEN date_sub(NOW(), interval 1 day )
AND date_sub(NOW(), INTERVAL 8 hour)
I would not depend on the exact time when the script is being run. Instead, use arithmetic based on the current date:
SELECT dt.name
FROM daily_tracking dt
WHERE completed < date_sub(curdate(), interval (24 - 14) hour) or
completed > date_add(curdate(), interval 10 hour);
This will work on a given day, regardless of the time the script is run.
You can also write it this way, which I prefer...
SELECT dt.name
FROM daily_tracking dt
WHERE dt.completed BETWEEN CURDATE() - INTERVAL 14 HOUR AND CURDATE() + INTERVAL 10 HOUR;