How to get epoch function in Oracle - sql

select *
from sample_table scr
where extract('epoch' from systimestamp - scr.created_date)/60 > :defaultTimeOut
This is a postgres query. Trying to convert this query into oracle.
How do I convert epoch in oracle?
TIA.

You are actually trying to convert an interval (ie the difference between two dates) to a number of seconds. Assuming that created_date is of date datatype, one method is:
select *
from sample_table
where (sysdate - created_date) * 24 * 60 * 60 > :defaultTimeOut
Rationale: in Oracle, substracting two dates returns a decimal number that represents the difference in days. You can multiply that by 24 (hours per day), 60 (minutes per hour) and 60 (seconds per minute) to convert that to a number of seconds, then compare it to the target value.
If created_date is of timestamp datatype, you can cast it to a date first:
select *
from sample_table
where (sysdate - cast(created_date as date)) * 24 * 60 * 60 > :defaultTimeOut

Related

How to extract the hour of day from an epoch and count each instance that occurs during that hour?

I have a question that I feel is pretty straight forward but is giving me some issues.
I have a column in table X called event_time which is an epoch. I am wanting to extract the hour of day out of that and count the number of rides that have occurred during that hour.
So the output will end up being a bar chart with x values 0-24 and the Y being the number of instances that occur (which is bike rides for example).
Here is what I have now, that isn't giving me the correct output:
select extract(hour from to_timestamp(start_time)::date) as hr,
count(*) as ct
from x
group by hr
order by hr asc
Any hints or help are appreciated.
Thanks
You can use arithmetic:
select floor( (start_time % (24 * 60 * 60)) / (60 * 60) ) as hour,
count(*)
from x
group by hour;
Or convert to a date/time and extract the hour:
select extract(hour from '1970-01-01'::date + start_time * interval '1 second') as hour, count(*)
from x
group by hour;

duration between two columns with format hhmmss

How can I determine the difference / duration between two columns, which have the following format:
HHMMSS.
Leading zeros are swallowed by the system (Oracle DB).
0 o'clock is a "0".
3 o'clock is "300".
23:50 clock is "235000".
Whatever, 24:00 is also reported as "240000".
That means I have the column "Start" and the column "End".
How can you determine the duration using SQL?
Convert the values to seconds using arithmetic:
select hhmmss, floor(hhmmss / 10000) * 60 * 60 + (mod(hhmmss, 10000) / 100) * 60 + mod(hhmmss, 100)
from (select 235000 as hhmmss from dual) x
Then subtract the seconds. That will give you the duration in seconds.
With your column names, this would look like:
select ( (floor(end / 10000) * 60 * 60 + (mod(end, 10000) / 100) * 60 + mod(end, 100)) -
(floor(start / 10000) * 60 * 60 + (mod(start, 10000) / 100) * 60 + mod(start, 100))
) as diff_seconds
To convert this back to a string, you can use to_char():
select to_char(date '2000-01-01' + diff_seconds * interval '1' second, 'HH24:MI:SS')
You can achieve the duration in hour/minutes/seconds using to_date as following:
Select round(
(to_date(lpad(end,6,'0'),'HHMISS')
- to_date(lpad(start,6,'0'),'HHMISS') ) * 24
, 2) diff_in_hours
-- multiply it by another 60 to get diff in minutes (24*60)
-- multiply it by another 3600 to get diff in seconds (24*3600)
from your_table
To get the output in HHMISS just add it to sysdate
Select
TO_CHAR(Trunc(sysdate) + (to_date(lpad(end,6,'0'),'HHMISS')
- to_date(lpad(start,6,'0'),'HHMISS')) , 'HH:MI:SS') as diff_HHMISS
from your_table
Cheers!!

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.

Getting a count of timestamps that fall into each 5 minute bucket

I have a table of timestamps. I'd like to group them into 5 minute buckets and get the count of timestamps in that bucket. I having some trouble getting the SQL quite right. I am using Postgres. It's telling me the timestamp column in the last line doesn't exist, but it's defined as an alias.
SELECT
TIMESTAMP WITH TIME ZONE 'epoch' +
INTERVAL '1 second' * round(extract('epoch' from my_timestamp) / 300) * 300
as timestamp,
count(my_timestamp)
FROM logs
GROUP BY
round(extract('epoch' from timestamp) / 300)
I think your GROUP BY is off. Try this:
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + INTERVAL '1 second' * round(extract('epoch' from my_timestamp) / 300) * 300 as timestamp,
count(*)
FROM (values (now())) logs(my_timestamp)
GROUP BY timestamp

SQL Redshift difference in days between 2 timestamps

In my Redshift table I have 2 columns that stores timestamp values: start_date_time and end_date_time.
I need to find the difference between start_date_time and end_date_time, such that, if difference is 1 day, then result should be 1. If diff is 12 hours, than result should be 0.5, if diff is 8 hours - than 0.3333 etc.
How can I do this? I tried to use datediff function: but that will not return what I wanted:
select datediff(day,'2011-12-31 8:30:00','2011-12-31 20:30:00') as day_diff;
Will result in 0. But I need 0.5, because difference is 12 hours.
Then just do it in hours and divide by 24.0?
DATEDIFF(HOUR,'2011-12-31 8:30:00','2011-12-31 20:30:00') / 24.0
Or for better granularity, in minutes or seconds?
DATEDIFF(MINUTE,'2011-12-31 8:30:00','2011-12-31 20:30:00') / 1440.0
DATEDIFF(SECOND,'2011-12-31 8:30:00','2011-12-31 20:30:00') / 86400.0
Use a smaller time unit:
select datediff(hour, '2011-12-31 8:30:00', '2011-12-31 20:30:00')/24.0 as day_diff;
Or:
select datediff(minute, '2011-12-31 8:30:00', '2011-12-31 20:30:00')/(24.0 * 60) as day_diff;
Or:
select datediff(second, '2011-12-31 8:30:00', '2011-12-31 20:30:00')/(24.0 * 60 * 60) as day_diff;