Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DE062B5DBC6 for ; Thu, 9 Jun 2011 13:20:22 -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 79833-09 for ; Thu, 9 Jun 2011 16:20:15 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f46.google.com (mail-fx0-f46.google.com [209.85.161.46]) by mail.postgresql.org (Postfix) with ESMTP id 99230B5DBCA for ; Thu, 9 Jun 2011 13:20:15 -0300 (ADT) Received: by fxm17 with SMTP id 17so1093563fxm.19 for ; Thu, 09 Jun 2011 09:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=pZ26B5AlPprbgIy/DttiZuWVSC12UIJmLX6g7JOhNbI=; b=nP1mAhUx29sVWR8zMAFq2zvUEr+UbwxepTRCwafFBY1dKLmx0RRK7zOWYa0Os6s/BQ O0Q3ujJwLegpO2m9r62sfABjrCOPBC7MW+LQwyrnhJzzhkqohmHKNgMjOcQOhBEXGM9I Ae0MPK17KHtuOMHIMXq7FqfSGV9qdQZ69BxXw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=mGx+8uuRiuK3vgU4uC3M0Vg6K20T9l5ZxlR1EPXbvKAGhT26ud/z50fsjKQRAosgqU A/+33qCl32sjCU2i1yhfcMtzXvZzXgrgn8efKFyM2YlFdDitO5Ywsun8R256fvLHkkw9 Xr8nml0km97W2w+v4S1Gjbdc2HnAK6urM+7O8= MIME-Version: 1.0 Received: by 10.223.100.15 with SMTP id w15mr1029496fan.11.1307636414748; Thu, 09 Jun 2011 09:20:14 -0700 (PDT) Received: by 10.223.1.73 with HTTP; Thu, 9 Jun 2011 09:20:14 -0700 (PDT) In-Reply-To: <4DF0CDFC.4070802@gmx.net> References: <4DF0CDFC.4070802@gmx.net> Date: Thu, 9 Jun 2011 09:20:14 -0700 Message-ID: Subject: Re: need help with some aggregation magic From: Richard Broersma To: Andreas Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.898 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001 X-Spam-Level: X-Archive-Number: 201106/26 X-Sequence-Number: 35492 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; -- Regards, Richard Broersma Jr.