get minutes and seconds from query in postgresql - postgresql

I need a time in minutes and seconds. If minutes is greater than 60 I then add to minutes, but do not convert hours
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'HH24:MI:SS')
as duration from table;
Output:
DURATION
02:50:21
Required Output:
DURATION
170:21
even i have tried another query (without HH24) but i get below out put:
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'MI:SS') as duration from table;
Output:
DURATION
50:21
Here we can see from 1st query output 02 hrs means 120 minutes + 50minutes =170 minutes and seconds same as it is.
is it possible directly getting from query or not?

you don't need to_char here:
select (sum(seconds) / 60)::text || ':' || (sum(seconds) % 60) ...;

Related

PostgreSQL query for elapsed interval

I am trying to query PostgreSQL database for rows where interval has elapsed from the last run. Main columns for this purpose are processed_at as timestamptz and frequency (in minutes) as integer.
I am failing with operators, since not many of them can operate together timestamp & integer.
Can someone please propose a query that would solve this? Thank you very much for help
From here Date/time operators:
timestamp + interval → timestamp
Add an interval to a timestamp
timestamp '2001-09-28 01:00' + interval '23 hours' → 2001-09-29 00:00:00
select now() + (10::varchar || ' min')::interval;
?column?
-------------------------------
2021-10-15 09:05:37.927163-07
--Or in your case. If I'm following you are adding the interval.
select processed_at + (frequency::varchar || ' min')::interval;
The query takes the integer value of minutes and converts it to an interval of minutes that can be added to the timestamp.
Further explanation, || is the Postgres concatenation operator and ::varchar, ::interval are casting shorthand.
UPDATE
I keep forgetting about the make_*() functions for date/time/interval
--A shorter version
select processed_at + make_interval(mins => frequency);
Saves all the casting.

How to write 40 minutes in Postgresql interval

I read the Postgresql documentation but I still can't understand how to write the interval when I'm adding data to the table. I want to put 40 minutes through OmniDBs interface but I'm oblivious about the format to write it in.
there's several ways. here are some.
select
interval '40' minute ,-- using the minute keyword
interval '40 minute' ,-- string description
interval '0:40' ,-- hours and minutes
40 * interval '1m' ,-- using arithmetic on 1 minute
'0:40:00'::interval ,-- hours minutes and seconds, with a cast
'40 minutes'::interval ,-- string with a cast
interval '2400' ;-- that many seconds
note: the keyword must be minute but in a string description minutes, m, and min also work.
for inserts into a table where postgres knows the type that the values should be coerced to the cast or interval keyword may be optional
insert into times (t) values
(interval '40' minute ), -- using keyword interval also needed
('40 minute' ), -- string description
('0:40' ), -- 0 hours and 40 minutes
(40 * interval '1 m' ), -- for arithmetic interval is needed
('0:40:00' ), -- h:m:s
('40 minutes' ), -- string desc.
('2400' ); -- 2400 seconds
documentation here
https://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
But that doesn't describe the use of the keyword form.

How to sum UTC format and secs

in my DB2 database, I have the UTC data in the format 1160307144500000 and a field which indicates the time difference respect to local date, for example 3600 in seconds.
I need sum to UTC date the 3600 secs so I can get 1160307154500000.
Here is some DB2 code snippet
TIMESTAMP('1900-01-01 00:00:00')
+ INT(SUBSTR(utc_data,1,3)) YEAR
+ INT(SUBSTR(utc_data,4,2)) + 1 MONTH
+ INT(SUBSTR(utc_data,6,2)) DAY
+ INT(SUBSTR(utc_data,8,2)) HOUR
+ INT(SUBSTR(utc_data,10,2)) MINUTE
+ INT(SUBSTR(utc_data,12,2)) SECOND
+ 1000*INT(SUBSTR(utc_data,15,3)) MICROSECOND
+ diff_data SECOND
I'll leave the code to transform the timestamp back to the original format up to you :)

Convert to Seconds

I am creating a report in SSRS where part of my solutions required me to convert the following result of the query to be calculate in seconds.
What do I use to get the results of this query in Seconds
Format(Fields!Created.Value, "HH:mm") >= "18:00" And Format(Fields!Created.Value, "HH:mm") <= "18:59" , DateAdd(dateinterval.Hour, 14, Fields!Created.Value)
I already tried Minute but it did not work
Thanks,

Perl: how to know if 24hrs have passed?

I have a timestamp field on a mysql table , i want to know if that timestamp is 24hrs old or more. What would be the best way to do that in Perl?
SQL that would return the timestamp 24 hours ago.
SELECT UNIX_TIMESTAMP(NOW() - INTERVAL 24 HOUR)
Now if your timestamp is < the timestamp returned by the above SQL , 24 hours have passed.
The best thing is to let the database do the work. See SQL statement to select all rows from previous day for an example.
By timestamp I am assuming it's Unix timestamp, i.e. seconds since epoch.
my $ts = 1393662619;
my $day_24hr = 24 * 60 * 60; ## seconds in 24 hrs
my $prev_time = time() - $day_24hr; ## 24hours ago
if ( $ts < $prev_time ) {
print "timestamp is 24 hour old";
}
You can use localtime for that.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($unix_timestamp);