pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Broersma <richard.broersma@gmail.com>
To: Andreas <maps.on@gmx.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: need help with some aggregation magic
Date: Thu, 9 Jun 2011 09:20:14 -0700
Message-ID: <BANLkTi=M4QNH1QCaoH6k+dCVL3j5wczEnQ@mail.gmail.com> (raw)
In-Reply-To: <4DF0CDFC.4070802@gmx.net>
References: <4DF0CDFC.4070802@gmx.net>

On Thu, Jun 9, 2011 at 6:43 AM, Andreas <maps.on@gmx.net> wrote:

> I have a log-table that stores events of users and projects like this
> ( user_id integer, project_id integer, ts timestamp, event_type integer )
>
> I need an aggregated list of worktime per user, per project, per day.
>
> The users can switch projects during the day so I can't work this out with
> min(ts) and max(ts).

SELECT user_id, project_id, date_trunc(  'day', ts ) as event_day,
              MIN( ts ) AS event_start, MAX( ts ) AS event_end,
              MAX( ts ) - MIN( ts ) AS duration
  FROM Loggingtable
GROUP BY user_id, project_id, date_trunc( 'day', ts )
ORDER BY date_trunc( 'day', ts ), user_id, project_id;


-- 
Regards,
Richard Broersma Jr.



view thread (7+ messages)  latest in thread

Message-ID: <BANLkTi=M4QNH1QCaoH6k+dCVL3j5wczEnQ@mail.gmail.com>
Permalink:  ../BANLkTi=M4QNH1QCaoH6k+dCVL3j5wczEnQ@mail.gmail.com/
Also on:    postgresql.org/message-id/BANLkTi=M4QNH1QCaoH6k+dCVL3j5wczEnQ@mail.gmail.com

 · 

reply

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: richard.broersma@gmail.com, maps.on@gmx.net
  Subject: Re: need help with some aggregation magic
  In-Reply-To: <BANLkTi=M4QNH1QCaoH6k+dCVL3j5wczEnQ@mail.gmail.com>

* 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