SQL Multiple record : Time Scheduler - sql

I have problem about combining tables in store procedure.
Note : field "Time" is varchar
First table (tbTime)
Time
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00
12:30:00
13:00:00
13:30:00
14:00:00
14:30:00
15:00:00
15:30:00
16:00:00
16:30:00
17:00:00
17:30:00
18:00:00
18:30:00
19:00:00
19:30:00
20:00:00
Second table (tbClassRsv)
select * from tbclassrsv where transdate='2014-02-05 00:00:00' and status<>'DEL'
transDate time until status studentCode tutor class description userID
2014-02-05 16:48:14 17:48:14 OPN ET-7201 ET-444 ROOM 01 try ADMIN
I want the result with condition schedule like this
Time Student
08:00:00 -
08:30:00 -
09:00:00 -
09:30:00 -
10:00:00 -
10:30:00 -
11:00:00 -
11:30:00 -
12:00:00 -
12:30:00 -
13:00:00 -
13:30:00 -
14:00:00 -
14:30:00 -
15:00:00 -
15:30:00 -
16:00:00 -
16:30:00 ET-7201
17:00:00 ET-7201
17:30:00 ET-7201
18:00:00 ET-7201
18:30:00 -
19:00:00 -
19:30:00 -
20:00:00 -
Thanks for reading or answer ^_^
GBU
I`ve tried this
select t.time,
isnull(
(select c.studentCode
from tbclassrsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and t.time>=c.time
and t.time<=c.until
),'-') [Student]
The result is....
Time Student
08:00:00 -
08:30:00 -
09:00:00 -
09:30:00 -
10:00:00 -
10:30:00 -
11:00:00 -
11:30:00 -
12:00:00 -
12:30:00 -
13:00:00 -
13:30:00 -
14:00:00 -
14:30:00 -
15:00:00 -
15:30:00 -
16:00:00 -
16:30:00 -
17:00:00 ET-7201
17:30:00 ET-7201
18:00:00 -
18:30:00 -
19:00:00 -
19:30:00 -
20:00:00 -

Try this. you were not converting your varchar times to datetime so that your time comparisons would work.
select t.time,
isnull(
(select c.studentCode
from tbClassRsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and DateAdd(MINUTE, 30, Convert(datetime, t.time))>= Convert(datetime, c.time)
and Convert(datetime, t.time) <= Convert(datetime, c.until)
),'-') from [tbTime] t

What you need to do is round c.time down to the nearest 30 minutes interval, and round c.until up to the nearest interval. This way your where clause will have the correct range.
To do the rounding you will need to convert the times to datetime which you can do like so:
CAST(CONVERT(varchar,THE_TIME_AS_VARCHAR,121) AS datetime)
Then you can round down to the nearest 30 minutes like so:
DATEADD(mi, DATEDIFF(mi, 0, THE_TIME_AS_DATETIME)/30*30, 0)
And round up like so:
DATEADD(mi, DATEDIFF(mi, 30, THE_TIME_AS_DATETIME)/30*30, 0)
Applying all that to your existing code would give you this:
select t.time,
isnull(
(select c.studentCode
from tbclassrsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and t.time>= DATEADD(mi, DATEDIFF(mi, 0, CAST(CONVERT(varchar,c.time,121) AS datetime))/30*30, 0)
and t.time<= DATEADD(mi, DATEDIFF(mi, 30, CAST(CONVERT(varchar,c.until,121) AS datetime))/30*30, 0)
),'-') [Student]

Related

Calculate difference between endTime from ID 1 and startTime from ID 2

id startTime endTime
1 2022-12-3 13:00:00 2022-12-3 14:00:00
2 2022-12-3 14:00:00 2022-12-3 14:30:00
3 2022-12-3 15:00:00 2022-12-3 15:15:00
4 2022-12-3 15:30:00 2222-12-3 16:30:00
5 2022-12-3 18:30:00 2022-12-3 19:00:00
SELECT startTime, endTime,
(TIMESTAMPDIFF(MINUTE, startTime , endTime) = '60') AS MinuteDiff
FROM booking
OUTPUT:
id startTime endTime MinuteDiff
1 2022-12-3 13:00:00 2022-12-3 14:00:00 1
2 2022-12-3 14:00:00 2022-12-3 14:30:00 0
3 2022-12-3 15:00:00 2022-12-3 15:15:00 0
4 2022-12-3 15:30:00 2022-12-3 16:30:00 1
5 2022-12-3 18:30:00 2022-12-3 19:00:00 0
I am calculating the difference between the startTime and endTime of ID 1, how to calculate the difference between the endTime of ID 1 and the startTime of ID 2, and so on?
Do try this one:
If you want your last row to be included in your result, use LEFT JOIN, if you don't want to include the last row use 'JOIN'.
SELECT d.`id`,
d.`endTime`,
IFNULL(d1.`startTime`,d.`endTime`),
IFNULL(TIMESTAMPDIFF(MINUTE, d.endTime, d1.startTime),0) FROM date_table d LEFT
JOIN date_table d1 ON d1.`id`=d.`id`+1
Or you can use following with Windows Functions:
SELECT
id,
endTime,
lead(startTime) over (order by id) nextStartDate,
TIMESTAMPDIFF(MINUTE,endTime,lead(startTime) over (order by id)) as timeDiff
FROM
date_table d;

Oracle sql create agenda

I have a table with interval dates and times. Can i create a full list with this data?
Table example:
Start_Date, End_Date, Start_Time, End_Time, Interval
01-jun-2021 02-jun-2021 08:00 10:00 30
03-jun-2021 04-jun-2021 10:00 12:00 15
Result:
01-jun-2021 08:00
01-jun-2021 08:30
01-jun-2021 09:00
01-jun-2021 09:30
02-jun-2021 08:00
02-jun-2021 08:30
02-jun-2021 09:00
02-jun-2021 09:30
03-jun-2021 10:00
03-jun-2021 10:15
03-jun-2021 10:30
03-jun-2021 11:00
03-jun-2021 11:15
03-jun-2021 11:30
03-jun-2021 11:45
04-jun-2021 10:00
04-jun-2021 10:15
04-jun-2021 10:30
04-jun-2021 11:00
04-jun-2021 11:15
04-jun-2021 11:30
04-jun-2021 11:45
Thanks.
This is a handy place to use a recursive CTE:
with cte (start_date, end_date, interval) as (
select to_date(start_date||start_time, 'DD-Mon-YYYYHH24:MI'), to_date(end_date||end_time, 'DD-Mon-YYYYHH24:MI'), interval
from t
union all
select cte.start_date + cte.interval * interval '1' minute, end_date, interval
from cte
where cte.start_date < end_date
)
select cast(start_date as timestamp)
from cte
order by start_date;
Here is a db<>fiddle.
You can use a recursive CTE, but the logic has to skip to the next day when you reach the end time; so this works:
with rcte (date_time, end_date, start_int, end_int, step_int) as (
select
start_date + to_dsinterval('0 ' || start_time || ':00'),
end_date,
to_dsinterval('0 ' || start_time || ':00'),
to_dsinterval('0 ' || end_time || ':00'),
interval * interval '1' minute
from your_table
union all
select
case
when date_time + step_int < trunc(date_time) + end_int
then date_time + step_int
else trunc(date_time) + interval '1' day + start_int
end,
end_date,
start_int,
end_int,
step_int
from rcte
where date_time + step_int < end_date + end_int
)
select date_time
from rcte
order by date_time
DATE_TIME
-------------------
2021-06-01 08:00:00
2021-06-01 08:30:00
2021-06-01 09:00:00
2021-06-01 09:30:00
2021-06-02 08:00:00
2021-06-02 08:30:00
2021-06-02 09:00:00
2021-06-02 09:30:00
2021-06-03 10:00:00
2021-06-03 10:15:00
2021-06-03 10:30:00
2021-06-03 10:45:00
2021-06-03 11:00:00
2021-06-03 11:15:00
2021-06-03 11:30:00
2021-06-03 11:45:00
2021-06-04 10:00:00
2021-06-04 10:15:00
2021-06-04 10:30:00
2021-06-04 10:45:00
2021-06-04 11:00:00
2021-06-04 11:15:00
2021-06-04 11:30:00
2021-06-04 11:45:00
db<>fiddle showing the anchor member including converting the times and interval to real day to second intervals types for later use; the anchor and recursive members with all the intermediate columns; and finally just this version with a single column.
You can format the resulting date value however you want, of course.

SQL Server : get start time and end time with in the multiple night shift

How can I get the Start and End time of this list? I can add date to this time and can get by min and max but you can see row 3 have next day shift but it will come under same date because it is night shift
I have added normal day shift employee also get the logic right
EmployeeId ShiftDate ShiftStartTime ShiftEndTime
-----------------------------------------------------
20040 2017-11-01 21:00:00 23:00:00
20040 2017-11-01 23:00:00 00:30:00
20040 2017-11-01 00:30:00 06:00:00
20124 2017-11-01 09:00:00 16:30:00
20124 2017-11-01 16:30:00 22:00:00
20124 2017-11-01 22:00:00 22:30:00
I need it like below:
EmployeeId ShiftDate ShiftStartTime ShiftEndTime
----------------------------------------------------
20040 2017-11-01 21:00:00 06:00:00
20124 2017-11-01 09:00:00 22:30:00
In a commercial environment we solved this by attaching a FLAG to each shift. The Flag would indicate the 'Reporting Date' of the Shift...The Flag would have have a value of 1 if the 'Reporting / Administrative date' was the 'next' day. 0 for the same day. -1 for the previous day (which we never used...depends on your scenario)
I modified your table to show a possible SHIFTS table, which should also have a NAME column I guess (like Morning, Afternoon, Day, Night shift etc)
ReportFlag ShiftStartTime ShiftEndTime
1 21:00:00 23:00:00
1 23:00:00 00:30:00
0 00:30:00 06:00:00
0 09:00:00 16:30:00
0 16:30:00 22:00:00
1 22:00:00 22:30:00
Notice how I added 1 - to say that 'this shift' is actually considered to be on the 'next' day.
Then you can use your flag value 0,1 to add to DATE functions in your queries too

How to add leading zero in a number in Oracle with to_number function

create or replace FUNCTION OT_TIMMING_CHECK
(V_OT_ROOM_ID NUMBER,V_OPERATION_DATE DATE,V_START_TIME VARCHAR2,
V_END_TIME VARCHAR2)
RETURN NUMBER AS
V_STATUS_1 VARCHAR2(10);
V_STATUS_2 VARCHAR2(10);
V_STATUS_3 VARCHAR2(10);
V_STATUS_4 VARCHAR2(10);
V_STATUS_5 VARCHAR2(10);
V_STATUS_6 VARCHAR2(10);
C_END_TIME NUMBER;
C_START_TIME NUMBER;
F_OPERATION_DATE DATE;
V_COUNT NUMBER :=0;
b_start_time varchar(10);
BEGIN
SELECT OPERATION_DATE INTO F_OPERATION_DATE FROM OT_THEATRE_STATUS_TBL1
WHERE OPERATION_DATE = V_OPERATION_DATE AND ROWNUM<2 ;
SELECT (case when (substr(V_START_TIME,1,instr(V_START_TIME,':')-1))='00'
then 24
else to_number(substr(V_START_TIME,1,instr(V_START_TIME,':')-1)) end) INTO
C_START_TIME from dual;
SELECT (case when (substr(V_END_TIME,1,instr(V_END_TIME,':')-1))='00' then
24 else to_number(substr(V_END_TIME,1,instr(V_END_TIME,':')-1)) end) INTO
C_END_TIME from dual;
IF V_OT_ROOM_ID=1 AND F_OPERATION_DATE = V_OPERATION_DATE THEN
WHILE (C_START_TIME < C_END_TIME)
LOOP
SELECT STATUS_1 INTO V_STATUS_1 FROM OT_THEATRE_STATUS_TBL1
WHERE START_TIME=concat(to_char(C_START_TIME),':00:00')
AND END_TIME= REPLACE(concat(to_char(C_START_TIME+1),':00:00'),24,'00');
IF(V_STATUS_1 ='Booked' OR V_STATUS_1='Pending') then
RETURN 1;
ELSE
C_START_TIME:= TO_CHAR(C_START_TIME+1);
V_COUNT:=V_COUNT+1;
end if;
END LOOP;
RETURN 0;
END IF ;
END;
Database structure is like this
V_START_TIME V_END_TIME OPERATION_Date STATUS_1 OT_ROOM_ID_1
01:00:00 01:00:00 24-Mar-17 AVAILABLE 1
02:00:00 02:00:00 24-Mar-17 LAPSED 1
03:00:00 03:00:00 24-Mar-17 AVAILABLE 1
04:00:00 04:00:00 24-Mar-17 AVAILABLE 1
05:00:00 05:00:00 24-Mar-17 AVAILABLE 1
06:00:00 06:00:00 24-Mar-17 AVAILABLE 1
07:00:00 07:00:00 24-Mar-17 AVAILABLE 1
08:00:00 08:00:00 24-Mar-17 AVAILABLE 1
09:00:00 09:00:00 24-Mar-17 AVAILABLE 1
10:00:00 10:00:00 24-Mar-17 AVAILABLE 1
11:00:00 11:00:00 24-Mar-17 AVAILABLE 1
12:00:00 12:00:00 24-Mar-17 AVAILABLE 1
13:00:00 13:00:00 24-Mar-17 BOOKING 1
14:00:00 14:00:00 24-Mar-17 AVAILABLE 1
15:00:00 15:00:00 24-Mar-17 AVAILABLE 1
16:00:00 16:00:00 24-Mar-17 AVAILABLE 1
17:00:00 17:00:00 24-Mar-17 AVAILABLE 1
18:00:00 18:00:00 24-Mar-17 AVAILABLE 1
19:00:00 19:00:00 24-Mar-17 AVAILABLE 1
20:00:00 20:00:00 24-Mar-17 AVAILABLE 1
21:00:00 21:00:00 24-Mar-17 AVAILABLE 1
22:00:00 22:00:00 24-Mar-17 AVAILABLE 1
23:00:00 23:00:00 24-Mar-17 AVAILABLE 1
00:00:00 00:00:00 24-Mar-17 AVAILABLE 1
Earlier V_START_TIME like '1:00:00:00' but now it is leading with 0. Then problem comes. Now it is returning null. How to come out from this problem.
Number won't be having leading zero's
Can you try the following:
LPAD(to_number(SUBSTR('01:00:00',1,instr('01:00:00',':')-1)),2, 0)
reference:
How to display the leading zero's in a number of oracle
yes, try it
SELECT (
CASE
WHEN (SUBSTR('01:00:00',1,instr('01:00:00',':')-1))='00'
THEN '24'
ELSE LPAD(SUBSTR('01:00:00',1,instr('01:00:00',':')-1),2,'0')
END)
FROM dual;

How to convert a timestamp from one timezone to other

I have a table with TIMESTAMP column in Teradata.
I want to consider the values stored in this column as if they are from 'America Pacific' timezone and convert it into GMT timestamp.
I tried,
select timestamp_col,
timestamp_col at 'GMT' timestamp_col_gmt,
timestamp_col at 'America Pacific' timestamp_col_pac,
from table_name;
timestamp_col timestamp_col_gmt timestamp_col_pac
------------------- ------------------------- -------------------------
2014-10-27 22:02:29 2014-10-27 22:02:29+00:00 2014-10-27 15:02:29-07:00
2013-11-28 22:55:27 2013-11-28 22:55:27+00:00 2013-11-28 14:55:27-08:00
2014-09-19 00:23:56 2014-09-19 00:23:56+00:00 2014-09-18 17:23:56-07:00
2013-06-18 00:39:18 2013-06-18 00:39:18+00:00 2013-06-17 17:39:18-07:00
But it looks like, it is considering timestamp_col as originally being in GMT.
What I want is something like this.
timestamp_col timestamp_col_gmt
------------------- -------------------
2013-11-28 22:55:27 2013-11-29 06:55:27 --date belongs to PST. 8 hour difference
2014-10-27 22:02:29 2014-10-28 05:02:29 --date belongs to PDT. 7 hour difference
2014-09-19 00:23:56 2014-09-19 07:23:56 --date belongs to PDT. 7 hour difference
2013-06-18 00:39:18 2013-06-18 07:39:18 --date belongs to PDT. 7 hour difference
I want to consider daylight savings also.
Using dnoeth's query, for sometime during the timezone switch, the result is incorrect.
with recursive y(timestamp_col) as
(
select timestamp_val
from x
union all
select timestamp_col + interval '1' hour
from y
where timestamp_col <= timestamp'2015-03-08 10:00:00'
),
x(timestamp_val) as
(
select timestamp'2015-03-08 00:00:00'
)
select timestamp_col,
cast(
(timestamp_col at 'America Pacific')
- (extract(timezone_hour from (timestamp_col at 'America Pacific')) * interval '1' hour)
as timestamp(0)) timestamp_col_gmt
from y
order by timestamp_col;
timestamp_col timestamp_col_gmt
------------------- -------------------
2015-03-08 00:00:00 2015-03-08 08:00:00 --PST, correct
2015-03-08 01:00:00 2015-03-08 09:00:00 --PST, correct
2015-03-08 02:00:00 2015-03-08 10:00:00 --Can be ignored
2015-03-08 03:00:00 2015-03-08 11:00:00 --PDT, should be 10:00:00
2015-03-08 04:00:00 2015-03-08 12:00:00 --PDT, should be 11:00:00
2015-03-08 05:00:00 2015-03-08 13:00:00 --PDT, should be 12:00:00
2015-03-08 06:00:00 2015-03-08 14:00:00 --PDT, should be 13:00:00
2015-03-08 07:00:00 2015-03-08 15:00:00 --PDT, should be 14:00:00
2015-03-08 08:00:00 2015-03-08 16:00:00 --PDT, should be 15:00:00
2015-03-08 09:00:00 2015-03-08 17:00:00 --PDT, should be 16:00:00
2015-03-08 10:00:00 2015-03-08 17:00:00 --PDT, correct
2015-03-08 11:00:00 2015-03-08 18:00:00 --PDT, correct
with recursive y(timestamp_col) as
(
select timestamp_val
from x
union all
select timestamp_col + interval '1' hour
from y
where timestamp_col <= timestamp'2015-11-01 10:00:00'
),
x(timestamp_val) as
(
select timestamp'2015-11-01 00:00:00'
)
select timestamp_col,
cast(
(timestamp_col at 'America Pacific')
- (extract(timezone_hour from (timestamp_col at 'America Pacific')) * interval '1' hour)
as timestamp(0)) timestamp_col_gmt
from y
order by timestamp_col;
timestamp_col timestamp_col_gmt
------------------- -------------------
2015-11-01 00:00:00 2015-11-01 07:00:00 --PDT, correct
2015-11-01 01:00:00 2015-11-01 08:00:00 --PDT, correct
2015-11-01 02:00:00 2015-11-01 09:00:00 --PST, should be 10:00:00
2015-11-01 03:00:00 2015-11-01 10:00:00 --PST, should be 11:00:00
2015-11-01 04:00:00 2015-11-01 11:00:00 --PST, should be 12:00:00
2015-11-01 05:00:00 2015-11-01 12:00:00 --PST, should be 13:00:00
2015-11-01 06:00:00 2015-11-01 13:00:00 --PST, should be 14:00:00
2015-11-01 07:00:00 2015-11-01 14:00:00 --PST, should be 15:00:00
2015-11-01 08:00:00 2015-11-01 15:00:00 --PST, should be 16:00:00
2015-11-01 09:00:00 2015-11-01 17:00:00 --PST, correct
2015-11-01 10:00:00 2015-11-01 18:00:00 --PST, correct
2015-11-01 11:00:00 2015-11-01 19:00:00 --PST, correct
Teradata stores Timestamps normalized to GMT/UTC/00:00 and displays them based on the session timezone.
Your system or user seems to be set to GMT as default.
This should return the expected data:
Get the data based on the 'America Pacific' time zone, subtract the time zone hour and cast it back to a Timestamp.
CAST((timestamp_col AT 'America Pacific')
- (EXTRACT(TIMEZONE_HOUR FROM (timestamp_col AT 'America Pacific')) * INTERVAL '1' HOUR)
AS TIMESTAMP(0))
Edit:
This returns the correct values for the switch to DST:
CAST(
(timestamp_col AT 'America Pacific')
- (EXTRACT(TIMEZONE_HOUR FROM ((timestamp_col + INTERVAL '7' HOUR) AT 'America Pacific')) * INTERVAL '1' HOUR)
AS TIMESTAMP(0)) timestamp_col_gmt
When it's applied to the switch back from DST:
2014-11-02 01:00:00 2014-11-02 08:00:00
2014-11-02 02:00:00 2014-11-02 10:00:00