current time minus mod_date_time - date

I want to be able to subtract the current time from the mod_date_time field.
This is what I have so far:
SELECT * FROM PROD_TRKG_TRAN.MOD_DATE_TIME - CURRENT_TIME WHERE USER_ID = '12345' * 24 * 60 "INTERVAL IN MINUTES"
Any help?

If you are using Oracle, you can use the following query:
SELECT (MOD_DATE_TIME - SYSDATE) * 24 * 60
FROM PROD_TRKG_TRAN
WHERE USER_ID = '12345';

Related

Extract() from Postgres to calculate minutes between 2 columns

Want to calculate minutes between to columns with start_time and end_time as timestamp without zone for two customers types, and then averange the result for each.
I tried to use extract() by using the following statement, but can't get the right result:
select avg(duration_minutes)
from (
select started_at,ended_at, extract('minute' from (started_at - ended_at)) as duration_minutes
from my_data
where customer_type = 'member'
) avg_duration;
Result:
avg
0.000
This run sucessfuly in BQ using the following:
select avg(duration_minutes) from
(
select started_at,ended_at,
datetime_diff(ended_at,started_at, minute) as duration_minutes
from my_table
where customer_type = "member"
) avg_duration
Result:
f0_
21.46
Wondering what might be failing in postgres?
extract(minute from ...) extracts the field with the minutes from the interval. So if the interval is "1 hour 26 minutes and 45 seconds" the result would be 26 not 86.
To convert an interval to the equivalent number of minutes, extract the total number of seconds using extract(epoch ...) and multiply that with 60.
select avg(duration_minutes)
from (
select started_at,
ended_at,
extract(epoch from (started_at - ended_at)) * 60 as duration_minutes
from my_data
where customer_type = 'member'
) avg_duration;
Note that you can calculate the average of an interval without the need to convert it to minutes:
select avg(duration)
from (
select started_at,
ended_at,
started_at - ended_at as duration
from my_data
where customer_type = 'member'
) avg_duration;
Depending on how you use the result, returning an interval might be more useful. You can also convert that to minutes using:
extract(epoch from avg(duration)) * 60 as average_minutes

Why does SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL; return FALSE in postgresql?

I am trying to compare two dates and return TRUE if the first date is less than '1 year 1 day' from the second date.
Using 361 days instead of '1 year 1 day' returns FALSE, but this makes sense based on why justify_interval('360 days'::interval) results '1 year'.
But when I run
SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL;
I get FALSE, and when I run
SELECT '2019-05-03'::timestamp - '1 year 1 day'::INTERVAL < '2018-05-07'::timestamp;
I get TRUE.
Why do these return different things?
I can't find it in the docs, but this is due to the way intervals represented and compared.
Note that:
select timestamp '2019-05-03' - timestamp '2018-05-07' < interval '366 day';
gives you the expected result of TRUE.
To compare two intervals, Postgres first converts the intervals to integers. This is done in a pretty naive way, where years are concerned:
/*
* interval_relop - is interval1 relop interval2
*
* Interval comparison is based on converting interval values to a linear
* representation expressed in the units of the time field (microseconds,
* in the case of integer timestamps) with days assumed to be always 24 hours
* and months assumed to be always 30 days. To avoid overflow, we need a
* wider-than-int64 datatype for the linear representation, so use INT128.
*/
So, your query is asking:
select 361 * 24 * 3600 * 1000000 < (1 * 12 * 30 * 24 * 3600 * 1000000) + (1 * 24 * 3600 * 1000000);
Or,
select 31,190,400,000,000 < 31,190,400,000,000;
which is obviously false. ^^

Filtering data older than 10 minutes

In MySQL I can use:
select *
from mytable
where created_at < DATE_SUB(NOW(), INTERVAL 1 MINUTE)
for filtering less than 10 minutes.
How to do that in PostgreSQL? The created at in style "2018-09-27 12:11:32".
select *
from mytable
where created_at < now()::timestamp - INTERVAL '10 min'

How to make query in postgresql which will find all records which are started more than 6 hours?

How to make query in postgresql which will find all records which are started more than 6 hours ago?
In my table I have a column which stores utc time of last time used in milliseconds (long integer).
SELECT *
FROM exams
WHERE started < HERE_I_NEED_UTC_IN_MILLISECONDS - 6 * 60 * 1000 * 1000;
select *
from exams
where started < extract(epoch from (current_timestamp - interval '6' hour))

Update a column by each value

Below is my table contains duration as HH:MM:SS
i could convert it into seconds by
SELECT (SUBSTRING(CAST('00:01:15' AS NVARCHAR(MAX)),1,2) * 360)
+ (SUBSTRING(CAST('00:01:15' AS NVARCHAR(MAX)),4,2) * 60)
+ SUBSTRING(CAST('00:01:15' AS NVARCHAR(MAX)),7,2)
and now i need to update that table into seconds by reading each row of records how?
like instead of having '00:01:15' i need each row of #DURATIONINSECONDS
Try like below
If your table have value like this
Table1
DurationInSeconds
00:01:15
00:00:15
00:01:16
00:01:17
and you want to Update the field DurationInSeconds into Seconds...
then just try like the below query....
update Table1 set DurationInSeconds = (SUBSTRING(CAST(DurationInSeconds AS NVARCHAR(MAX)),1,2) * 360) + (SUBSTRING(CAST(DurationInSeconds AS NVARCHAR(MAX)),4,2) * 60) + SUBSTRING(CAST(DurationInSeconds AS NVARCHAR(MAX)),7,2)
then it give the Result like below
SELECT * from Table1
DurationInSeconds
75
15
76
77