From: Jan Muszynski <postgres@jancm.org>
To: aarni@kymi.com
Cc: pgsql-sql@postgresql.org
Subject: Re: interval as hours or minutes ?
Date: Wed, 07 Feb 2007 17:09:41 -0500
Message-ID: <45CA07D5.9647.29301B0@postgres.jancm.org> (raw)
In-Reply-To: <200702071903.35126.aarni@kymi.com>
References: <200702071903.35126.aarni@kymi.com>
On 7 Feb 2007 at 19:03, Aarni Ruuhimäki wrote:
> Hi all,
>
> Could anyone please tell an easy way to get total hours or minutes from an
> interval ?
>
> SELECT SUM(stop_date_time - start_date_time) AS tot_time FROM work_times WHERE
> user_id = 1;
> tot_time
> -----------------
> 2 days 14:08:44
>
> I'd like to have this like ... AS tot_hours ...
> tot_hours
> -----------------
> 62
>
> and ... AS tot_minutes ...
> tot_minutes
> -----------------
> 3728
>
> Maybe even ... AS tot_hours_minutes_seconds
> tot_hours_minutes_seconds
> -----------------
> 62:08:44
>
>
> start_date_time and stop_date_time are stored as timestamp without time zone,
> using Pg 8.1.5 on CentOs 4.4
select extract(epoch from (timestamp '2007-02-07 16:24:00' - timestamp '2007-02-05
13:00:00')) as num_seconds;
num_seconds
------------
185040
(1 row)
select (extract(epoch from (timestamp '2007-02-07 16:24:00' - timestamp '2007-02-05
13:00:00')) * interval '1 second') as hours_minutes_seconds;
hours_minutes_seconds
-----------------------
51:24:00
(1 row)
select extract(epoch from (timestamp '2007-02-07 16:24:00' - timestamp '2007-02-05
13:00:01'))/60 as minutes;
minutes
------------------
3083.98333333333
(1 row)
select round(extract(epoch from (timestamp '2007-02-07 16:24:00' - timestamp '2007-02-05
13:00:01'))/60) as minutes;
minutes
---------
3084
(1 row)
select round(extract(epoch from (timestamp '2007-02-07 16:24:00' - timestamp '2007-02-05
13:00:01'))/360) as hours;
hours
-------
514
(1 row)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: postgres@jancm.org, aarni@kymi.com
Subject: Re: interval as hours or minutes ?
In-Reply-To: <45CA07D5.9647.29301B0@postgres.jancm.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox