I have a simple table with bunch of applicants, where they have their Start Time, End Time and Date data. I want to figure out who is available for a specific date within a Time Range.
Table below shows where Start and End Date Columns tells us they are booked for that Date/Time .........
AppID StartTime EndTime Date
H12 8:00 13:00 12/1/2013
H12 14:00 16:00 12/1/2013
H12 19:00 21:00 12/1/2013
H14 17:00 18:00 12/1/2013
H13 14:00 16:00 12/1/2013
H13 11:00 15:00 12/2/2013
H15 8:00 13:00 12/2/2013
So in the Table above how can I write a Query that will say...Give me all the applications for 12/1/2013 which are NOT working between 17:00 - 18:00? So basically it should return H12 & H13 (Because its time slot for 17-18 pm is not available within table for 12/1/2013).
This query returns the apps ID which were not working at a time range on a specific date.
SELECT
[AppID] = FreeApps
FROM table_name t1
WHERE AppID NOT IN (-- Not in the set of apps that were busy at the time range
SELECT AppID
FROM table_name t2
WHERE ((StartTime >= '17:00' AND StartTime <= '18:00')
OR (EndTime >= '17:00' AND EndTime <= '18:00'))
AND Date = '12/1/2013'
AND t1.AppID=t2.AppID)
GROUP BY AppID
;WITH t1 as
(
SELECT DISTINCT AppID
FROM <table>
WHERE date = '20130112'
)
SELECT AppID
FROM t1
WHERE
NOT EXISTS
(SELECT * FROM <table> t2
WHERE t2.STARTTIME < '18:00'
AND t2.ENDTIME > '17:00'
AND t1.AppID = t2.AppID
AND t2.date = '20130112'
)
Related
I need to retrieve data from a table from a given interval of time. My table is like this -
id
Start Time
End Time
1
06:30:00
07:00:00
2
06:45:00
07:15:00
3
13:15:00
14:00:00
4
09:30:00
10:15:00
Given interval of time - (05:00:00 - 10:00:00)
My Expectation -
id
Start Time
End Time
1
06:30:00
07:00:00
2
06:45:00
07:15:00
4
09:30:00
10:15:00
I need to get the id (4) as its start time in the given interval of time.
So what will be the query?
so far I can imagine this -
To get all the values between the given interval of time - (05:00:00 - 10:00:00) use:
select *
from tbl
where Start_Time between '05:00:00' and '10:00:00'
or End_Time between '05:00:00' and '10:00:00';
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ba828afe1a3ca9f21d6c104ea7dcfdff
Using comparison operator
SELECT * FROM tbl WHERE
(`start_time`>= '05:00:00' AND `start_time` <= '10:00:00') OR
(`end_time`>= '05:00:00' AND `end_time` <= '10:00:00')
I am really having issue showing the items. I have two different times(they are in UTC).
FOR Eg: startTime - 15:00:00(in UTC) and cutOffTime - 03:00:00(in UTC and supposed to be up to 3am UTC next morning). My local timezone is EST. The issue I am having is after 8pm(EST) when next day start at UTC. My items does not shows up. Here is my sql query. I am pulling startTime and cutOffTime from table2. MY server is in UTC as well.
SELECT a.id, a.column1, a.catID, b.startTime, b.cutOffTime, IFNULL(b.`column2`, 'everyday') as 'availableDays'
FROM table1 a
RIGHT JOIN table2 b on a.catID = b.catID
WHERE id = 012
and CASE WHEN b.startTime IS NOT NULL THEN CONCAT(CURDATE(),' ', b.startTime) < NOW() ELSE 1 END
and CASE WHEN b.cutOffTime IS NOT NULL THEN If((b.startTime is not NULL AND b.cutOffTime < b.startTime), DATE_ADD(CONCAT(CURDATE(),' ', b.cutOffTime), INTERVAL 24 HOUR), CONCAT(CURDATE(),' ', b.cutOffTime)) > NOW() ELSE 1 END
HAVING availableDays in ('everyday', 'wednesday')
Order by a.column1 ASC;
My table2 is like this:
catID Cat startTime cutOffTime
1. Test 15:00:00 03:00:00
2. Test2 null null
3. Test3 15:00:00 null
Suppose it is 8:30pm EST(which is 00:30:00 UTC) then I am expecting to get all the results. Now, It is only returning me 2 and 3.
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.
edit: I have a table with start and end time fields. I would group the intervals between the common hours. It's possible?
Assume this table:
time_begin time_end
07:00 18:00
09:00 12:00 (this interval is in the first line)
17:00 18:00 (this interval is in the first line)
I would like to return the lines:
time_begin time_end
09:00 12:00
17:00 18:00
Thanks
Try this query.
SELECT time_begin, time_end
FROM table_name
WHERE time_begin < (SELECT time_end
FROM table_name
LIMIT 1)
AND time_begin > (SELECT time_begin
FROM table_name
LIMIT 1)
AND time_end < (SELECT time_end
FROM table_name
LIMIT 1)
AND time_end > (SELECT time_begin
FROM table_name
LIMIT 1)
This query returns all time intervals which are between the time interval in the first row.
I have written 2 queries and I need to look at combining them, into what I think would have to be a cursor. The first query looks like this-
DECLARE #Date DATETIME
SET #Date = '12-July-2014'
SELECT Date AS SaturdayDate
, DayOfWeek
FROM (
SELECT DATE
, DATENAME(DW,DATE) as DayOfWeek
, ROW_NUMBER() OVER (ORDER BY Date) AS rownum
FROM tblCalender
WHERE DATENAME(DW,DATE) = 'Saturday'
AND Date > #Date
) AS t
WHERE t.rownum % 4 = 0
AND DATEPART(YEAR, Date) = DATEPART(Year, GETDATE())
returns every 4th Saturday for the rest of the year past the 12th July -
SaturdayDate |DayOfWeek
2014-08-09 00:00:00.000 |Saturday
2014-09-06 00:00:00.000 |Saturday
2014-10-04 00:00:00.000 |Saturday
2014-11-01 00:00:00.000 |Saturday
2014-11-29 00:00:00.000 |Saturday
2014-12-27 00:00:00.000 |Saturday
This is perfect for what I need, then i need to run an update query using the result of the above as the #SaturdayDate parameter (need it to cycle through and update every record)
UPDATE dbo.tblStaffRota
SET StartTime = '11:00:00'
WHERE (EmployeeID IN ('JJJ','HSW', 'GPH', 'DVD'))
AND (StartTime = '10:30:00')
AND (Date BETWEEN #SaturdayDate - 6 AND #SaturdayDate)
So the update query will cycle through the result of the first query updating each date with the new start time, not sure how to progress with this and all the answers I've found haven't quiet helped. Hopefully this is clear enough. Thanks
*additional note - The update needs to look at the days in the previous week to each saturday that appears and change whatever the start time is.
Sample of tables being used.
Based on this the update would change the start time on the 18th and 19th if the Saturday was in the list.
Looks like you need something like (Note: below script have not been tested) ..
Modified based on posted sample data:
DECLARE #Date DATETIME
SET #Date = '12-July-2014'
UPDATE STAFFROTA
SET StartTime = '11:00:00'
FROM dbo.tblStaffRota STAFFROTA
INNER JOIN
(
SELECT DATEADD(d, -6, Date) AS SaturdayDateStart, Date AS SaturdayDateEnd
, DayOfWeek
FROM (
SELECT DATE
, DATENAME(DW,DATE) as DayOfWeek
, ROW_NUMBER() OVER (ORDER BY Date) AS rownum
FROM tblCalender
WHERE DATENAME(DW,DATE) = 'Saturday'
AND Date > #Date
) AS t
WHERE t.rownum % 4 = 0
AND DATEPART(YEAR, Date) = DATEPART(Year, GETDATE())
) SAT
ON STARFFROTA.[Date] BETWEEN SAT.SaturdayDateStart AND SAT.SaturdayDateEnd
WHERE (STARFFROTA.EmployeeID IN ('JJJ','HSW', 'GPH', 'DVD'))
AND (STARFFROTA.StartTime = '10:30:00')