Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZA1Js-0005T9-39 for pgsql-admin@arkaria.postgresql.org; Tue, 30 Jun 2015 19:35:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZA1Jq-0006My-Qp for pgsql-admin@arkaria.postgresql.org; Tue, 30 Jun 2015 19:35:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z95Zo-0000KR-8d for pgsql-admin@postgresql.org; Sun, 28 Jun 2015 05:56:24 +0000 Received: from mbob.nabble.com ([162.253.133.15]) by makus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1Z95Zh-0002uz-MW for pgsql-admin@postgresql.org; Sun, 28 Jun 2015 05:56:22 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mbob.nabble.com (Postfix) with ESMTP id 60353FEAD09 for ; Sat, 27 Jun 2015 22:57:01 -0700 (PDT) Date: Sat, 27 Jun 2015 22:56:16 -0700 (MST) From: litu16 To: pgsql-admin@postgresql.org Message-ID: <1435470976136-5855509.post@n5.nabble.com> Subject: SUM all timeelapse WHERE timetype = 'Break' but only the rows that are after(below) timetype = 'Start' MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.3 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org ))Hi all, This is my table... This is the SQL statement to create the same table... * CREATE TABLE tbl_ebscbspa_log05 ( pcnum smallint NOT NULL, fnserial serial NOT NULL, fnname character varying NOT NULL, timestmp timestamp without time zone DEFAULT clock_timestamp(), timeelapse interval, timetype character varying, timeindex real, CONSTRAINT table_ebscb_spa_log05_pkey PRIMARY KEY (fnserial) ) WITH ( OIDS=FALSE ); ALTER TABLE tbl_ebscbspa_log05 OWNER TO postgres;* How can I SUM all timeelapse WHERE timetype = 'Break' AND that are placed after(below) the last(descendent) timetype = 'Start'? So in the table above Ill get both blue square colored rows summed up. So I get 'totalbreak' = 00-00-00 02:00:00.000 How would be the syntax to select all possible 'Breaks' after the last(descendent) 'Start' and sum them up? (lets say the max possible 'Breaks' are nine). I have try this, resulting NULL *SELECT t.timeelapse FROM tbl_ebscbspa_log05 t WHERE t.timetype = 'Break' AND NOT EXISTS (SELECT 1 FROM tbl_ebscbspa_log05 WHERE timetype = 'Start' AND 'timestmp' > 't.timestmp') INTO v_timeelapse_break; IF FOUND THEN NEW.timeelapse := v_timeelapse_break;* Pls I would like to know how to SUM THEM UP, not just SELECT THEM, hope some good PostgreSQL fellow programmer could give me a hand with it. Thanks Advanced. -- View this message in context: http://postgresql.nabble.com/SUM-all-timeelapse-WHERE-timetype-Break-but-only-the-rows-that-are-after-below-timetype-Start-tp5855509.html Sent from the PostgreSQL - admin mailing list archive at Nabble.com. -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin