Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 2B7F52E0098 for ; Wed, 12 Dec 2007 13:31:40 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 03224-09 for ; Wed, 12 Dec 2007 13:31:31 -0400 (AST) Received: from smaug.vex.net (smaug.vex.net [208.76.104.132]) by postgresql.org (Postfix) with ESMTP id 9BD562E0099 for ; Wed, 12 Dec 2007 13:31:32 -0400 (AST) Received: from dilbert (dilbert.druid.net [207.35.13.14]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smaug.vex.net (Postfix) with ESMTP id C0F2717194; Wed, 12 Dec 2007 12:31:29 -0500 (EST) Date: Wed, 12 Dec 2007 12:31:29 -0500 From: "D'Arcy J.M. Cain" To: "Gary Chambers" Cc: pgsql-sql@postgresql.org Subject: Re: Query Assistance Message-Id: <20071212123129.ad22c7ce.darcy@druid.net> In-Reply-To: <302670f20712120858k210dd812l8b232ce98d5d162f@mail.gmail.com> References: <302670f20712120858k210dd812l8b232ce98d5d162f@mail.gmail.com> X-Mailer: Sylpheed 2.4.5 (GTK+ 2.12.0; i386--netbsdelf) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/71 X-Sequence-Number: 29763 On Wed, 12 Dec 2007 11:58:20 -0500 "Gary Chambers" wrote: > All... > > I have a simple table in PostgreSQL 8.2.5: > > CREATE TABLE power_transitions ( > -- Transition ID (PK) > tid integer NOT NULL, > -- State ID (0 = Unknown, 1 = Online (AC power), 2 = Offline (Battery) > sid smallint NOT NULL, > -- Timestamp of transition > statetime timestamp without time zone DEFAULT now() NOT NULL, > -- Is this a real outage? > is_outage boolean DEFAULT true NOT NULL > ); > > It contains a log of power outages (transitions). I'd like to create > query that returns a transition offline time and associated return to > online time. Is there a better way of handling this? I am open to > schema change suggestions. Thanks very much! Have you considered this? CREATE TABLE power_transitions ( -- Transition ID (PK) tid integer NOT NULL, -- Timestamp of power off ( starttime timestamp without time zone DEFAULT now() NOT NULL, -- Timestamp of power on ( endtime timestamp without time zone, -- Is this a real outage? may not be needed. is_outage boolean DEFAULT true NOT NULL ); The is_outage bool could be handled with special timestamps (e.g. EPOCH) but I am not sure what it signifies to you. Of course you have to deal with false transitions but I don't know what your information capture system is so I can't work out all the details but this seems like a good base for what you want. Every row that has a valid start and end time is a complete record of an outage. I am also thinking of a scheme that uses two tables but I don't really know your environment or requirements. I am assuming that you spend more time querying the table than updating it. If not your problem isn't your database, it's your power plant. :-) -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.