Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 59009B5DC02 for ; Thu, 9 Jun 2011 18:39:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 35571-02-3 for ; Thu, 9 Jun 2011 21:39:27 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f174.google.com (mail-iw0-f174.google.com [209.85.214.174]) by mail.postgresql.org (Postfix) with ESMTP id B6386B5DC26 for ; Thu, 9 Jun 2011 18:39:22 -0300 (ADT) Received: by iwn34 with SMTP id 34so1672750iwn.19 for ; Thu, 09 Jun 2011 14:39:22 -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:from:date :message-id:subject:to:cc:content-type; bh=WV0h5pDWSr3XEqw951gfHmzWGNkfosA4RqlprplSaN8=; b=LtDN6N57P0y0bbE8ONmRlnXwdRqrxiwYCqo0B6MN3aj9Xt59W1g4eeZC9/8myQwIlq GskHhEMkO+bB3vj4jlZYTiV5RJsil0p53GXWxPPHWh/LCY4alL5aGWfpnY1ESeM1Atgk 8IzPkYxsNvphwN2OaeH7rs109PeoQ/QVJ3Io0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=NUqWpFov+ypmO8WC/5O9Lqpm+p7QnRDmjW7cMDMOEd8H4E75UkwG8+kv5fhBgdtLTI LmTMj2/XKx3inc80w5Z16C3bXRflSIliBC3Qk50xtMC5EiudC2w+MULmk/CEywGXKXFc rHHVvlUdxr+bbupxf8vkwbqGrEoDhJEFDq2Rk= Received: by 10.231.83.133 with SMTP id f5mr1439596ibl.114.1307655561117; Thu, 09 Jun 2011 14:39:21 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.149.132 with HTTP; Thu, 9 Jun 2011 14:39:01 -0700 (PDT) In-Reply-To: <4DF0CDFC.4070802@gmx.net> References: <4DF0CDFC.4070802@gmx.net> From: Kevin Crain Date: Thu, 9 Jun 2011 14:39:01 -0700 Message-ID: Subject: Re: need help with some aggregation magic 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=-0.345 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001 X-Spam-Level: X-Archive-Number: 201106/28 X-Sequence-Number: 35494 Try this: select user_id, project_id, date_trunc, sum(sum) FROM (select user_id, project_id, date_trunc('day', ts), SUM(duration) FROM (select user_id, project_id, a.ts, ((SELECT MIN(b.ts) FROM log b WHERE b.ts>a.ts AND (date_trunc('day',a.ts)=date_trunc('day',b.ts)))-a.ts) AS duration from log a order by user_id, project_id, ts) AS foo group by user_id, project_id, ts) AS day_set group by user_id, project_id, date_trunc order by user_id, project_id, date_trunc; -Kevin Crain On Thu, Jun 9, 2011 at 6:43 AM, Andreas wrote: > hi, > 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). > > Is there a clever way to get this with SQL ? > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >