Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C15161336971 for ; Mon, 7 Feb 2011 18:13:39 -0400 (AST) 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 05252-02 for ; Mon, 7 Feb 2011 22:13:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from polaris.pinpointresearch.com (cerberus.pinpointresearch.com [66.7.238.130]) by mail.postgresql.org (Postfix) with ESMTP id 558A513364DE for ; Mon, 7 Feb 2011 18:13:32 -0400 (AST) Received: from betelgeuse.pinpointresearch.com (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id C40AFE00EC84; Mon, 7 Feb 2011 14:13:30 -0800 (PST) Message-ID: <4D506E8A.30107@pinpointresearch.com> Date: Mon, 07 Feb 2011 14:13:30 -0800 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: stayler@washoecounty.us CC: pqsql list Subject: Re: Cumulative result with increment References: <1297113095.21916.76.camel@shop.telecom.co.washoe.nv.us> In-Reply-To: <1297113095.21916.76.camel@shop.telecom.co.washoe.nv.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/24 X-Sequence-Number: 35100 On 02/07/2011 01:11 PM, Shawn Tayler wrote: > Hello, > > I am struggling with what is probably a simple problem but I am coming > up blank. In the interest of full disclosure I am not a very savy > programmer by any stretch. > > I have a table of data from an external source which contains numbers of > events per operating group per hour per day, kind of like this: > > Group | events | duration | timestamp > > The Group, events, and duration (milliseconds) are all integers in char > format. Timestamp is as stated. The records are at 1 hour increments. > I need to sum the events and duration fields in 8 hour (shifts, mid to > 8AM, 8AM to 4PM, etc). > > Id like the output to be in table format ala: > > Group | events | duration | date | shift > ------+---------+------------+-----------+------- > 100 | 26 |00:00:25.00 |2011-01-01 | Day > > I have a function to convert the duration to an interval already and can > make one to do the shift labeling. Its the rest that has me stumped. > > Any suggestions or direction? > > As always, your help is greatly appreciated. > I'm not sure exactly what you want but it sounds like you just want an output column that has the shift instead of timestamp. You can then sum on that column. Don't know what your shifts are called, but this will give you dog-, day- and night-shifts based on your times: case when extract(hour from '2011-01-12 1600'::timestamptz)< 8 then 'dog' when extract(hour from now())< 16 then 'day' else 'night' end as shiftname This can be used for grouping as well as display. Cheers, Steve