Where date between a and b performance - sql

I have come across a query which has me curious whether the programmer was show boating or whether there is merit to the way it has been done in terms of performance. I have no clue as to why the from time is 01:59 rather than 00:00, this would actually remove some of the results that would actually want to be included.
This is the where clause of the query
WHERE REPORTDATE BETWEEN TRUNC(SYSDATE - 21) + 01 / 24 + 59 / (24 * 60) + 59 / (24 * 60 * 60)
AND TRUNC(SYSDATE) + 23 / 24 + 59 / (24 * 60) + 59 / (24 * 60 * 60)
and if my math is correct, is the same as
WHERE REPORTDATE BETWEEN to_date('13/04/2017 01:59','dd/mm/yyyy hh24:mi')
AND to_date('04/05/2017 23:59','dd/mm/yyyy hh24:mi')
Is there any benefit in the first calculated where clause over the second?

You can use interval literals to get rid of all the arithmetic and simplify the query:
WHERE REPORTDATE BETWEEN TRUNC( SYSDATE ) - INTERVAL '20 22:00:01' DAY TO SECOND
AND TRUNC( SYSDATE ) + INTERVAL '00 23:59:59' DAY TO SECOND
or
WHERE REPORTDATE BETWEEN TRUNC( SYSDATE ) - INTERVAL '21' DAY
+ INTERVAL '01:59:59' HOUR TO SECOND
AND TRUNC( SYSDATE ) + INTERVAL '00 23:59:59' DAY TO SECOND
or
WHERE REPORTDATE >= TRUNC( SYSDATE ) - INTERVAL '20 22:00:01' DAY TO SECOND
AND REPORTDATE < TRUNC( SYSDATE ) + INTERVAL '1' DAY

It is hard to imagine a performance difference, based on different ways of calculating constants in a query.
I would write this using something like this:
WHERE REPORTDATE >= CAST(TIMESTAMP '2017-04-13 02:00:00' as DATE) and
REPORTDATE < DATE '2017-05-05'
If you are going to include date/time constants, use the built-in mechanisms that support standard formats.
or for more flexibility based on the current date:
WHERE REPORTDATE >= TRUNC(sysdate) - 21 + 2 / 24 AND
REPORTDATE < TRUNC(sysdate) + 1
(or, if 1:59 is really intended . . . then TRUNC(sysdate) - 21 + (1 * 60 + 59) / (24 * 60).)

Related

Convert Timestamp to minutes in Oracle after a subtraction

I'm trying to get the minute value from a Timestamp after a subtraction
First I made a subtraction:
Current Time - Target Time(INSP_FIN_DT)
(
TO_TIMESTAMP(TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') -
TO_TIMESTAMP(INSP_FIN_DT,'YYYY-MM-DD HH24:MI:SS')
) AS REST_TIME
the output:
+00 00:09:44.000000
What I need:
09
I already tried
SELECT SUBSTR(REST_TIME, 7,2)
But the return is ever 00, even If I convert it to Char like:
SELECT SUBSTR(TO_CHAR(REST_TIME),7,2)
How can I solve it by query?
Information here https://dba.stackexchange.com/questions/53924/how-do-i-get-the-difference-in-minutes-from-2-timestamp-columns
select
round(
(SYSDATE - cast(<other_timestamp> as date))
* 24 * 60
) as diff_minutes
from <some_table>;
For my current implementation, I'll never have more than a hour, in this case I used the bellow solution:
SYSTIMESTAMP - TO_TIMESTAMP(INSP_FIN_DT,'YYYY-MM-DD HH24:MI:SS') AS REST_TIME
Select..
EXTRACT(MINUTE FROM REST_TIME) AS REST_MINUTES
In case of more than 60 minutes, I should use:
(EXTRACT(HOUR FROM REST_TIME) * 60)
+ EXTRACT(MINUTE FROM REST_TIME) AS REST_MINUTES

Oracle epoch date function for the previous month

I have my table with EPOCH date system, i want to select data from previous month only. I have googled a lot but the only results i find is related to datetime system. Can you please help?
I will use this SQL Query to import data to Power BI.
You can convert your date range to an epoch using ( date_to_convert - DATE '1970-01-01' ) * 24 * 60 * 60 (assuming your epoch is in seconds since 1970).
You want to find values that are greater-than-or-equal-to the start of the previous month and before the start of the current month:
SELECT *
FROM your_table
WHERE epoch_column >= ( ADD_MONTHS( TRUNC(SYSDATE,'MM'), - 1 ) - DATE '1970-01-01' ) * 24 * 60 * 60
AND epoch_column < ( TRUNC( SYSDATE, 'MM' ) - DATE '1970-01-01' ) * 24 * 60 * 60
If you use midnight of the last day of the previous month as your upper bound then you will miss all values that are on that last day but are after midnight.
You can convert a unix epoch (number of seconds since January 1st, 1970) to a date as follows :
TO_DATE('01/01/1970', 'dd/mm/yyyy') + epoch_column / 60 / 60 / 24
The division turns the epoch into a number of days, that can be added to the original date.
If you want to filter this on the previous month, then :
TO_DATE('01/01/1970', 'dd/mm/yyyy') + epoch_column / 60 / 60 / 24
BETWEEN ADD_MONTHS(TRUNC(sysdate, 'mm'), -1)
AND LAST_DAY(ADD_MONTHS(TRUNC(sysdate, 'mm'), -1))
Or better yet :
epoch_column
BETWEEN
(
ADD_MONTHS(TRUNC(sysdate, 'mm'), -1)
- TO_DATE('01/01/1970', 'dd/mm/yyyy')
) * 60 * 60 * 24
AND (
LAST_DAY(ADD_MONTHS(TRUNC(sysdate, 'mm'), -1))
- TO_DATE('01/01/1970', 'dd/mm/yyyy')
) * 60 * 60 * 24
This solution should be more efficient, as no operation is performed on the column being filtered, hence it should be able to take advantage of an existing index on epoch_column.

Oracle SQL round up time interval to next day

How to round up time interval to next day in Oracle SQL?
select apppackage
, numtodsinterval(
sum( trunc(extract (day from (periods)) * 86400
+ extract (hour from (periods)) *3600
+ extract (minute from (periods))*60
+ extract (second from (periods)))
), 'SECOND') as retention_period
, count(apppackage) as users
from retentions
where apppackage = 'com.Freesoul.Rotter'
group by apppackage;
The output of this is
'com.Freesoul.Rotter' '+2969 04:32:47.000000' '3'
and desired output is
'com.Freesoul.Rotter' '2970' '3'
but if the output of query is
'com.Freesoul.Rotter' '+2969 00:00:00.000000' '3'
then desired output is
'com.Freesoul.Rotter' '2969' '3'
column period is of INTERVAL DAY(9) TO SECOND(6) type
and i won't mind if the retention_period is changed to number datatype.
I'll be grateful if anyone can suggest change in my query to attain the desired output.
The result of your sum is in seconds, so you don't really need to convert it to an interval at all. Just divide by 60*60*24 to get the answer in days, and round it up with ceil():
select apppackage
, ceil(sum( trunc(extract (day from (periods)) * 86400
+ extract (hour from (periods)) *3600
+ extract (minute from (periods))*60
+ extract (second from (periods)))
) / 86400) as retention_period
, count(apppackage) as users
from retentions
where apppackage = 'com.Freesoul.Rotter'
group by apppackage;
Demo with artificial data in a CTE just to mimic your expected results, for both scenarios:
-- CTE for sample data
with retentions (apppackage, periods) as (
select 'com.Freesoul.Rotter', interval '+2967 04:32:47.000000' day(9) to second(6) from dual
union all
select 'com.Freesoul.Rotter', interval '1' day from dual
union all
select 'com.Freesoul.Rotter', interval '1' day from dual
union all
select 'com.Freesoul.XYZ', interval '+2967 00:00:00.000000' day(9) to second(6) from dual
union all
select 'com.Freesoul.XYZ', interval '1' day from dual
union all
select 'com.Freesoul.XYZ', interval '1' day from dual
)
-- actual query
select apppackage
, ceil(sum( trunc(extract (day from (periods)) * 86400
+ extract (hour from (periods)) *3600
+ extract (minute from (periods))*60
+ extract (second from (periods)))
) / 86400) as retention_period
, count(apppackage) as users
from retentions
where apppackage = 'com.Freesoul.Rotter'
-- extra clause for dummy data
or apppackage = 'com.Freesoul.XYZ'
group by apppackage;
APPPACKAGE RETENTION_PERIOD USERS
------------------- ---------------- ----------
com.Freesoul.XYZ 2969 3
com.Freesoul.Rotter 2970 3
Your expected output shows a plain number. If you actually want it as an interval, but as the whole number of days, just pass ceil'd number into numtodsinterval or more simply (and usually faster for some reason) multiply by interval '1' day.
With the same dummy data:
select apppackage
, ceil(sum( trunc(extract (day from (periods)) * 86400
+ extract (hour from (periods)) *3600
+ extract (minute from (periods))*60
+ extract (second from (periods)))
) / 86400) * interval '1' day as retention_period
, count(apppackage) as users
...
APPPACKAGE RETENTION_PERIOD USERS
------------------- --------------------- ----------
com.Freesoul.XYZ +2969 00:00:00.000000 3
com.Freesoul.Rotter +2970 00:00:00.000000 3
As #mathguy pointed out, you probably don't need or want the trunc() call in there; that is removing the fractional seconds from each period before they are summed, which sounds insignificant but could easily affect the result you get.
How about this?
CASE WHEN numtodsinterval(extract (day from periods), 'DAY') = periods THEN
extract (day from periods)
ELSE
extract (day from periods) + 1
END

How to compare date format in where clause

I would like to extract data where all the records were created after 19:00:00 yesterday until now. What I have so far is :
Select * from table where lstupdt >=trunc(sysdate -1);
How to specify after 19:00:00 hours?
Hmmm . . . How about this?
where lstupdt >= trunc(sysdate - 1) + 19 / 24
Or, alternatively, to be more accurate:
where lstupdt >= trunc(sysdate - 1, 'day') + interval '19' hour
or:
where lstupdt >= trunc(sysdate, 'day') - interval '5' hour

Calculate Average Time Over 24 hour period

I'm working in Teradata and am trying to calulate the average time a job completes.
Data Values:
Job Name Start Date End Date End Time
D_BDW_CCIP_SRM_LD 10/10/2012 10/11/2012 01:41:49
D_BDW_CCIP_SRM_LD 10/9/2012 10/10/2012 00:19:56
D_BDW_CCIP_SRM_LD 10/8/2012 10/8/2012 23:37:18
D_BDW_CCIP_SRM_LD 10/5/2012 10/5/2012 23:39:47
D_BDW_CCIP_SRM_LD 10/4/2012 10/4/2012 23:42:47
D_BDW_CCIP_SRM_LD 10/3/2012 10/3/2012 23:41:54
The average is coming back with 16:07 instead of 00:07. What I need to happen is that the calculations where the job finishes next day understands that the time expanded.
In Excel I could do this by adding one day to the end time and then averaging and displaying as a time.
How do I do this in Teradata?
This is such an interesting question! UPDATED with correct syntax: Assuming your START_DATE and END_DATE are DATE values and END_TIME is a TIME value, here is a solution:
select cast( avg( case
when start_date <> end_date
then extract(second from end_time)
+ extract(minute from end_time) * 60
+ extract(hour from end_time) * 3600
+ 86400
else extract(second from end_time)
+ extract(minute from end_time) * 60
+ extract(hour from end_time) * 3600
end) mod 86400) as decimal(10,4))
* INTERVAL '00:00:01.00' HOUR TO SECOND as avg_time
from your_table
The CASE expression "adds" one day (86,400 seconds) as you suggested when using Excel to determine the average seconds since midnight into an intermediate result and converted into a TIME column.
To be fair, I received help from the Teradata Forum formatting the result, but I like this so much I'll be using it myself.
This seems to do the trick, but I'd be interested in seeing if there is another way.
SELECT job_name,
case when avg_end_time_in_minutes > 60*24 then avg_end_time_in_minutes - 60*24
else avg_end_time_in_minutes end as avg_adjusted,
case when max_end_time_in_minutes > 60*24 then max_end_time_in_minutes - 60*24
else max_end_time_in_minutes end as max_adjusted,
CAST((CAST(avg_adjusted / 60 AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))||':'||
CAST((CAST((avg_adjusted / 60 MOD 1)*60 AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))
avg_adjusted_time,
CAST((CAST(max_adjusted / 60 AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))||':'||
CAST((CAST((max_adjusted / 60 MOD 1)*60 AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))
max_adjusted_time
FROM (
SELECT job_name,
AVG(end_time_in_minutes) avg_end_time_in_minutes,
MAX(CAST(end_time_in_minutes AS DECIMAL(8,2))) max_end_time_in_minutes
FROM (
SELECT job_name,
CAST(substr(end_time, 1, 2) AS INTEGER)*60
+ CAST(substr(end_time, 4, 2) AS INTEGER)
+ cast(end_date - start_date as integer)*60*24 AS end_time_in_minutes
FROM dabank_prod_ops_tb.bdw_tables_load_tracker_view a
WHERE a.status = 'COMPLETED'
AND a.start_date BETWEEN CURRENT_DATE - 31 AND CURRENT_DATE -1
AND a.end_time IS NOT NULL
) a
GROUP BY 1
) b
First, figure out the number of seconds that the end time is from midnight on the start date. We can then use that to calculate the average number of seconds taken, and then add that to midnight to find the average end time.
select
avg(extract(second from end_time) + 60 *
(extract(minute from end_time) + 60 *
(extract(hour from end_time) + 24 *
(end_date - start_date))) as avg_duration_in_seconds
cast(avg_duration_in_seconds / 60 / 60 as integer) as avg_hours
mod(cast(avg_duration_in_seconds / 60 as integer), 60) as avg_minutes
mod(cast(avg_duration_in_seconds as integer), 60) as avg_seconds,
cast('00:00:00' as time) +
cast(avg_hours as interval hour) +
cast(avg_minutes as interval minute) +
cast(avg_seconds as interval second) as avg_end_time
from my_table
Be aware though that if the average ends up over 24 hours, avg_end_time will be something like 00:01:15 rather than 24:01:15.