Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 66223B5DC2D for ; Thu, 9 Jun 2011 13:55:45 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 07768-02-8 for ; Thu, 9 Jun 2011 16:55:38 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.22]) by mail.postgresql.org (Postfix) with SMTP id CABADB5DBE5 for ; Thu, 9 Jun 2011 13:55:37 -0300 (ADT) Received: (qmail invoked by alias); 09 Jun 2011 16:55:35 -0000 Received: from p57BB6483.dip.t-dialin.net (EHLO [192.168.1.113]) [87.187.100.131] by mail.gmx.net (mp072) with SMTP; 09 Jun 2011 18:55:35 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX18pjRwo0scpzc5hnomGPgsR3GbdAgBzEGOZIaRKPV 2SG3jsmV7HOj8y Message-ID: <4DF0FB0A.2020801@gmx.net> Date: Thu, 09 Jun 2011 18:55:38 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: Richard Broersma CC: pgsql-sql@postgresql.org Subject: Re: need help with some aggregation magic References: <4DF0CDFC.4070802@gmx.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/27 X-Sequence-Number: 35493 Am 09.06.2011 18:20, schrieb Richard Broersma: > On Thu, Jun 9, 2011 at 6:43 AM, Andreas 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.