Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6FBAB1337B54 for ; Tue, 8 Feb 2011 14:10:00 -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 73475-01 for ; Tue, 8 Feb 2011 18:09:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail2.washoecounty.us (mail2.washoecounty.us [148.186.3.147]) by mail.postgresql.org (Postfix) with ESMTP id B85B81337B6E for ; Tue, 8 Feb 2011 14:09:52 -0400 (AST) Received: from CIMTA ([148.186.120.91]) by mailpp2.washoecounty.us (8.14.4/8.14.4) with ESMTP id p18I9oKV016913 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 8 Feb 2011 10:09:50 -0800 Subject: Re: Cumulative result with increment From: Shawn Tayler Reply-To: stayler@washoecounty.us To: Steve Crawford , pqsql list In-Reply-To: <4D506E8A.30107@pinpointresearch.com> References: <1297113095.21916.76.camel@shop.telecom.co.washoe.nv.us> <4D506E8A.30107@pinpointresearch.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Organization: Washoe County Regional Communications System Date: Tue, 08 Feb 2011 10:09:50 -0800 Message-ID: <1297188590.32163.1.camel@shop.telecom.co.washoe.nv.us> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 X-OriginalArrivalTime: 08 Feb 2011 18:09:50.0499 (UTC) FILETIME=[60AD8F30:01CBC7BB] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2011-02-08_05:2011-02-08, 2011-02-08, 1970-01-01 signatures=0 X-Proofpoint-Spam-Reason: safe 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/32 X-Sequence-Number: 35108 Thanks Steve, That did the trick! I appreciate the help.... Shawn On Mon, 2011-02-07 at 14:13 -0800, Steve Crawford wrote: > 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 o= f > > 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=09 > > > > I have a function to convert the duration to an interval already and ca= n > > 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. > > >=20 > I'm not sure exactly what you want but it sounds like you just want an=20 > output column that has the shift instead of timestamp. You can then sum=20 > on that column. Don't know what your shifts are called, but this will=20 > give you dog-, day- and night-shifts based on your times: >=20 > 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 >=20 > This can be used for grouping as well as display. >=20 > Cheers, > Steve >=20