Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2Pbz-0001zv-6E for pgsql-sql@arkaria.postgresql.org; Thu, 25 Jul 2013 17:45:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V2Pby-0000RY-LO for pgsql-sql@arkaria.postgresql.org; Thu, 25 Jul 2013 17:45:58 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2Pbx-0000RR-Hc for pgsql-sql@postgresql.org; Thu, 25 Jul 2013 17:45:57 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=mail.ringways.co.uk) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2Pbu-0007Tt-2A for pgsql-sql@postgresql.org; Thu, 25 Jul 2013 17:45:56 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1V2Pbr-0002zg-7v for pgsql-sql@postgresql.org; Thu, 25 Jul 2013 18:45:51 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: value from max row in group by Date: Thu, 25 Jul 2013 18:45:51 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201307251845.51079.gary.stainburn@ringways.co.uk> X-Pg-Spam-Score: 2.0 (++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi folks, I need help please. I have a table of trip section details which includes a trip ID, start time as an offset, and a duration for that section. I need to extract the full trip duration by adding the highest offset to it's duration. I can't simply use sum() on the duation as that would not include standing time. Using the data below I would like to get: 1 | 01:35:00 2 | 01:35:00 3 | 01:06:00 4 | 01:38:00 5 | 01:03:00 6 | 01:06:00 from timetable=> select stts_id, stts_offset, stts_duration from standard_trip_sections order by stts_id, stts_offset; stts_id | stts_offset | stts_duration ---------+-------------+--------------- 1 | 00:00:00 | 00:18:00 1 | 00:19:00 | 00:26:00 1 | 00:47:00 | 00:13:00 1 | 01:13:00 | 00:22:00 2 | 00:00:00 | 00:18:00 2 | 00:20:00 | 00:09:00 2 | 00:29:00 | 00:17:00 2 | 00:50:00 | 00:13:00 2 | 01:13:00 | 00:22:00 3 | 00:00:00 | 00:20:00 3 | 00:28:00 | 00:15:00 3 | 00:44:00 | 00:22:00 3 | 00:48:00 | 00:20:00 4 | 00:00:00 | 00:20:00 4 | 00:28:00 | 00:15:00 4 | 00:48:00 | 00:13:00 4 | 01:01:00 | 00:13:00 4 | 01:18:00 | 00:20:00 5 | 00:00:00 | 00:18:00 5 | 00:20:00 | 00:09:00 5 | 00:29:00 | 00:17:00 5 | 00:50:00 | 00:13:00 6 | 00:00:00 | 00:15:00 6 | 00:20:00 | 00:13:00 6 | 00:33:00 | 00:13:00 6 | 00:46:00 | 00:20:00 (26 rows) timetable=> -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql