Calculate time difference ignorning non-work hours - sql

I am trying to calculate the difference between 2 datetime values where non-work hours are ignored. Originally it just looked at the difference and calculated it as minutes however It needs to count only hours between 9am and 8pm Monday to Friday and 9am - 1pm Saturday, ignoring all other times. I am on an oracle 10g system.
my code as it currently stands is as follows:
begin
debug.debug('sp_access');
update cl_case b
set time_to_sp_access =
(
select (x.date_created-e.date_created)*1440
from cl_case c, eventlog e, eventlog x
where c.id=e.case_id
and x.case_id=e.case_id
and b.id=e.case_id
and e.id=
( select min(id) from eventlog mini
where mini.case_id=e.case_id
and mini.cl_code in ('AAAA','BBBB','CCCC','DDDD')
)
and x.id=
( select min(id) from eventlog minix
where minix.case_id=e.case_id
and minix.cl_code in ('EEEE','FFF','GGG','HHHH','JJJJ','KKKK','LLLL')
)
)
where id in
( select unique case_id
from eventlog elog
where elog.sptime_needs_setting ='Y'
);
commit;
end sp_access;
How can I get this to count time between specified hours?
thanks

You could use a CASE expression in the WHERE clause. Since there are two datetime values, you need to use two case expressions.
For example, the CASE expression would evaluate as:
SQL> SELECT
2 CASE
3 WHEN TO_CHAR(SYSDATE, 'DY') BETWEEN '1' AND '5'
4 THEN TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
5 ||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
6 ELSE TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
7 ||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
8 END my_time
9 FROM dual;
MY_TIME
----------------------
11/24/2015 01:00:00 pm
The above example check the DAY for SYSDATE, and depending on it returns a datetime value.
using the above example, since you have two different datetime values to be compared as a date range condition, you will need two CASE expressions in your WHERE clause.
WHERE date_column
BETWEEN
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
END
AND
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
END

Related

Oracle SQL - Grouping based on time within one second

Sample Table:
create table sampledata as
select 78328696 pkid, 12848815 customer_id, to_date('10/19/2022 11:05:38 AM','MM/DD/YYYY HH:MI:SS AM') actdate, 0.5 units, to_timestamp('19-OCT-22 11.05.38.947750000 AM') datetime from dual
union all
select 78328697, 12848815, to_date('10/19/2022 11:05:39 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.05.39.024819000 AM') from dual
union all
select 78328698, 12848815, to_date('10/19/2022 11:05:39 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.05.39.050859000 AM') from dual
union all
select 78321196, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.879037000 AM') from dual
union all
select 78321197, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.909837000 AM') from dual
union all
select 78321199, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.931040000 AM') from dual
union all
select 78321200, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.952084000 AM') from dual
union all
select 78321201, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.971703000 AM') from dual
union all
select 78321202, 12978419, to_date('10/19/2022 9:13:56 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.56.993092000 AM') from dual
union all
select 78321203, 12978419, to_date('10/19/2022 9:13:57 AM','MM/DD/YYYY HH:MI:SS AM'), 1, to_timestamp('19-OCT-22 09.13.57.014174000 AM') from dual
union all
select 78330838, 13710675, to_date('10/19/2022 11:44:29 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.44.29.465212000 AM') from dual
union all
select 78330839, 13710675, to_date('10/19/2022 11:44:29 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.44.29.498326000 AM') from dual
union all
select 78330840, 13710675, to_date('10/19/2022 11:44:29 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.44.29.527076000 AM') from dual
union all
select 78331625, 13710675, to_date('10/19/2022 11:56:28 AM','MM/DD/YYYY HH:MI:SS AM'), 0.5, to_timestamp('19-OCT-22 11.56.28.726815000 AM') from dual
I am looking to aggregate transactions together and sum the units. However, they need to be in the same transaction and there is nothing that specifically denotes a transaction. They will all have an ACTDATE within 1 or perhaps 2 seconds. So I am looking to group the first 3 rows together based on CUSTOMER_ID and sum the units. The next 7 rows would be grouped together as one transaction as well. The tricky part is when I hit the last 4 rows, CUSTOMER_ID 13710675. Here, there are actually 2 transactions. One transaction consisting of 3 rows at 11:44 and then a single row transaction at 11:56.
I have considered doing a lead(ACTDATE) over(partition by..., and look at the time difference, but this gets convoluted as well as resource and execution time heavy considering the number of rows in the actual data. I looked at rounding the microseconds in order to get the ACTDATE to match and include it in the GROUP BY, but this sacrifices some accuracy, including in the example given. Can you recommend an easier method? Please notice that the PKID may skip a number. I will take the max(PKID) and the trunc(ACTDATE) so the output should be:
On recent versions of Oracle - since Oracle 12c - you can use match_recognize to do this:
select max(pkid) as pkid,
customer_id,
trunc(max(actdate)) as accdate,
sum(units) as units
from sampledata
match_recognize (
partition by customer_id
order by actdate
measures match_number() as match_num
all rows per match
pattern (start_tran same_tran*)
define same_tran as (actdate <= prev(actdate) + interval '2' second)
)
group by customer_id, match_num
PKID
CUSTOMER_ID
ACCDATE
UNITS
78328698
12848815
10/19/2022
1.5
78321203
12978419
10/19/2022
7
78330840
13710675
10/19/2022
1.5
78331625
13710675
10/19/2022
.5
fiddle
The data is partitioned by customer_id, and for each ID the rows are ordered by actdate. The pattern and definition together define what constitutes a matching set - within a time window in this case - and each of those sets is assigned a match number. The data can then be grouped by the customer ID and that match number, and aggregates can then be calculated.
You can change the interval to make the matching window larger or smaller if necessary. It would work ordering and defining using datetime instead of actdate - not sure why you have both or if one is more appropriate than the other.

sysdate range to whole value

Is it possible to turn the current time stamp to a whole number?
Example: If sysdate returns 1/19/2022 5:36:49 PM can I turn that to 1/19/2022 5PM since it falls in the 5PM range.
Here is my query
Select FACILITY, TRK_ID, LOT_DTTM, IN_QTY
from TRK_ID_LOT
WHERE facility in 'DP1DM5'
and trk_id like ('AE%')
and lot_dttm > sysdate - 1
EXAMPLE:
Truncate it to hours:
SQL> select trunc(to_date('1/19/2022 5:36:49 PM', 'mm/dd/yyyy hh:mi:ss pm'), 'hh') res
2 from dual;
RES
----------------------
01/19/2022 05:00:00 PM
SQL>
If you want to update rows, do so using the same function:
update your_table set
date_column = trunc(date_column, 'hh');

How to compare date with time in oracle

Trying to compare two dates with time.But comparison is not working.
SELECT *
FROM attendance
WHERE TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') >=
TO_DATE ('01/09/2019 04:30:00 PM', 'DD/MM/YYYY HH:MI:SS AM')
AND TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') <=
TO_DATE ('30/09/2019 10:00:00 PM', 'DD/MM/YYYY HH:MI:SS AM')
AND userid = '3825'
AND SUBSTR (checktime, -2, 2) = 'PM'
ORDER BY TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') ASC
I was expecting output equal or grater then 04:30 PM and less then or equal 10:00 PM.But this date comparison is not working.Here is the
Output of Code.I want my result includes date and time between mentioned periods.
Note:CHECKTIME datatype is varchar2.
I think you need data for all the days(01/09/2019 - 30/09/2019) and the time of the day should be between 04:30 PM and 10: PM.
You can achieve this using the following query:
SELECT
*
FROM
ATTENDANCE
WHERE
TRUNC(TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM'))
BETWEEN TO_DATE('01/09/2019', 'DD/MM/YYYY')
AND TO_DATE('30/09/2019', 'DD/MM/YYYY')
AND ( TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM') - TRUNC(TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM')) ) * 1440 -- converting difference into minutes
BETWEEN 990 -- 04:30 PM in minutes (16.5*60)
AND 1320 -- 10:00 PM in minutes (22*60)
AND USERID = '3825'
AND SUBSTR(CHECKTIME, - 2, 2) = 'PM'
ORDER BY
TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM') ASC;
Cheers!!
You can fix your format by using such a format containing TO_TIMESTAMP conversion
SELECT *
FROM attendance
WHERE checktime
BETWEEN TO_TIMESTAMP('01/09/2019 16:30:00.000000','dd/mm/yyyy hh24:mi:ss.ff')
AND TO_TIMESTAMP('30/09/2019 22:30:00.000000','dd/mm/yyyy hh24:mi:ss.ff')
AND userid = 3825
ORDER BY checktime;
Demo
EDIT : you had better to add a new column with timestamp datatype, and update your new column's data by using TO_TIMESTAMP conversion such as below :
UPDATE attendance
SET checktime2 = TO_TIMESTAMP('3/09/2019 5:38:36 PM','dd/mm/yyyy hh:mi:ss AM')
WHERE id = 3825
AND checktime = '3/09/2019 5:38:36 PM'

Oracle SQL Group By a String Aggregated Field

I'm having a problem grouping a field in my query. Here is an example of what I'm talking about:
Example:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 30
15-JAN-15 8:00 AM 25
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 10
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 20
Those with same time should be grouped as one, summing up their matl_size
Expected output:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 55
15-JAN-15 9:00 AM 40
Here is my SQL:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),
21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
Thank you for helping in advance!
Based on your sample data, this should do what you want:
select AIR_DT, DOL_GAP_TIME, sum(MATL_SIZE)
from table t
group by AIR_DT, DOL_GAP_TIME;
However, I have no idea what this has to do with the query in the question.
OK let's start with your initial query:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
I see a couple of problems. One, you're grouping by e.matl_size even though that is the column you want to SUM(). You don't want it in the GROUP BY. Second, your manner of getting the time from dol_pref_start_time is really odd. It looks like you want to round down to the hour, then just get the hour plus whether it is AM or PM. So this:
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time
can simply be this:
TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
Third, are your dates stored as dates? If so, why are you doing this?
AND a.air_dt LIKE '%15-JAN-15%'
It would be far better to do this:
AND TRUNC(a.air_dt) = date'2015-01-15'
or, if you have an index on a.air_dt, this:
AND a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
Putting this all together, we get something like this (note that I've also converted your joins to ANSI SQL joins):
SELECT TRUNC(a.air_dt) AS air_dt
, TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
, SUM(e.matl_size) AS matl_size
FROM order_implem_dtl_broadcast a INNER JOIN matl_mstr b
ON a.matl_id = b.matl_id
INNER JOIN matl_size_mstr e
ON b.matl_size_id = e.matl_size_id
WHERE a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
GROUP BY TRUNC(a.air_dt), TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM')
ORDER BY air_dt, TO_DATE(dol_gap_time, 'HH:MI AM'); -- using aliases in the ORDER BY, converting dol_gap_time to DATE for sorting

start/end date and time in oracle sql query

I need a query output like the below table;
This is a primary entry to a table and these records will be modified by a third party program which I have no control. Can anyone suggest a good sample?
ID | DATEIN | DATEOUT | STATUS
1 02.02.2014 00:00:00 02.02.2014 23:59:59 1
2 03.02.2014 00:00:00 03.02.2014 23:59:59 0
I tried
SELECT To_Char(To_Date(SYSDATE), 'dd-MM-yyyy hh:mm:ss PM'),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual
but this query throws an error ORA-00907: missing right parenthesis.
There is no need for PM if you want it to be in 24-hour format. And pay attention to the mask for minutes, it is mi, not mm as in your query. Also as already mentioned no need to convert SYSDATE to date as it is already of that datatype:
SELECT to_char(to_date(SYSDATE), 'dd-mm-yyyy HH24:mi:ss') date_in,
to_char(to_date(SYSDATE + 1) - INTERVAL '1' SECOND, 'dd-mm-yyyy HH24:mi:ss') date_out
FROM dual;
DATE_IN DATE_OUT
------------------- -------------------
11-03-2014 00:00:00 11-03-2014 23:59:59
You can do away with DATE_ADD and TO_DATE functions (SYSDATE is already a DATE, no need of conversion ) , and also use mi to show minute instead of mm which is format specifier for month as in:
SELECT To_Char(SYSDATE, 'dd-MM-yyyy hh:mi:ss PM'),
To_Char((SYSDATE + 1) + INTERVAL '-1' SECOND, 'dd-MM-yyyy hh:mi:ss PM')
FROM dual
I am not clear what you are trying to achieve from the above query but if parenthesis is your only problem then you gotta hit the query:
SELECT To_Char(To_Date((SYSDATE), 'dd-MM-yyyy hh:mm:ss PM')),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual