Re: need help with some aggregation magic

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: need help with some aggregation magic
Date: 2011-06-09 16:55:38
Message-ID: 4DF0FB0A.2020801@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Am 09.06.2011 18:20, schrieb Richard Broersma:
> On Thu, Jun 9, 2011 at 6:43 AM, Andreas<maps(dot)on(at)gmx(dot)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;
>
As far as I understand you calculate the duration as the difference
between the first and last event of a project per day.
There is a problem because a user can work from 08.00 to 10.00 on
project 1 and then from 10.00 to 12.00 on project 2 and then from 12.00
to 16.00 on project 1 again.
Then I get project 1 8 hours plus project 2 2 hours though the
user actually was just 8 hours there.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Edgardo Portal 2011-06-09 21:22:57 Re: need help with some aggregation magic
Previous Message Richard Broersma 2011-06-09 16:20:14 Re: need help with some aggregation magic