postgresql - How to subtract seconds from postgres datetime without having to add it in group by clause? -


say have column of type datetime value "2014-04-14 12:17:55.772" & need subtract seconds "2" seconds o/p "12:17:53".

userid  enddate                 seconds -------------------------------------------------------- 1   "2014-04-14 12:17:14.295"   512 1   "2014-04-14 12:31:14.295"   12 2   "2014-04-14 12:48:14.295"   2 2   "2014-04-14 13:22:14.295"   12 

& query is

select (enddate::timestamp - (seconds* interval '1 second')) seconds, userid user group userid 

now need group userid enddate & seconds added select query asking me add in group clause not give me correct o/p.

i expecting data in format need calculate start_time end_time & total seconds spent.

    user : 1    start_time            end_time        total (seconds)       "12:17"           "12:17"             1       "12:22"           "12:31"            512                                        total:   513    user : 2       "12:43"           "12:48"         288       "13:22"           "13:22"         1                                        total    289 

is there way avoid group clause in this?

like @imsop says, can use window function include total each user in query output:

select userid      , (enddate - (seconds * interval '1 second')) start_time      , enddate end_time      , seconds      , sum(seconds) on (partition userid) total   so23063314.user; 

then display parts of row you're interested in each subtotal line, , display total @ end of each block.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -