Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2Pta-0002jK-5D for pgsql-sql@arkaria.postgresql.org; Thu, 25 Jul 2013 18:04:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V2PtZ-0004pz-Kk for pgsql-sql@arkaria.postgresql.org; Thu, 25 Jul 2013 18:04:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2PtY-0004ps-UU for pgsql-sql@postgresql.org; Thu, 25 Jul 2013 18:04:08 +0000 Received: from mail-by2lp0238.outbound.protection.outlook.com ([207.46.163.238] helo=na01-by2-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V2PtU-0000Go-3W for pgsql-sql@postgresql.org; Thu, 25 Jul 2013 18:04:08 +0000 Received: from BY2PR05MB237.namprd05.prod.outlook.com (10.242.41.146) by BY2PR05MB240.namprd05.prod.outlook.com (10.242.41.147) with Microsoft SMTP Server (TLS) id 15.0.731.16; Thu, 25 Jul 2013 18:03:58 +0000 Received: from BY2PR05MB237.namprd05.prod.outlook.com ([169.254.15.11]) by BY2PR05MB237.namprd05.prod.outlook.com ([169.254.15.190]) with mapi id 15.00.0731.000; Thu, 25 Jul 2013 18:03:58 +0000 From: Venky Kandaswamy To: Gary Stainburn , "pgsql-sql@postgresql.org" Subject: Re: value from max row in group by Thread-Topic: [SQL] value from max row in group by Thread-Index: AQHOiV799cXiTB+z1EWv0UVVsteoTZl1rgYAgAABgvc= Date: Thu, 25 Jul 2013 18:03:58 +0000 Message-ID: <9d70bbaa0ce84bab8c0467c83fbc01c7@BY2PR05MB237.namprd05.prod.outlook.com> References: <201307251845.51079.gary.stainburn@ringways.co.uk>, <201307251857.10326.gary.stainburn@ringways.co.uk> In-Reply-To: <201307251857.10326.gary.stainburn@ringways.co.uk> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [70.42.188.130] x-forefront-prvs: 0918748D70 x-forefront-antispam-report: SFV:NSPM; SFS:(377454003)(189002)(199002)(15202345003)(74876001)(81542001)(47446002)(56776001)(69226001)(46102001)(76576001)(81342001)(33646001)(76482001)(56816003)(77096001)(54356001)(76796001)(53806001)(76786001)(54316002)(50986001)(83072001)(65816001)(47976001)(49866001)(47736001)(19580405001)(19580385001)(80022001)(74662001)(19580395003)(83322001)(74316001)(66066001)(31966008)(16406001)(74706001)(74366001)(4396001)(74502001)(63696002)(77982001)(59766001)(79102001)(51856001)(24736002); DIR:OUT; SFP:; SCL:1; SRVR:BY2PR05MB240; H:BY2PR05MB237.namprd05.prod.outlook.com; CLIP:70.42.188.130; RD:InfoNoRecords; MX:1; A:1; LANG:en; Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: adchemy.com X-Pg-Spam-Score: -2.6 (--) 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 You can use Postgres WINDOW functions for this in several different ways. F= or example, one way of doing it: select stts_id, last_value(stts_offset) over (partition by stts_id order = by stts_offset desc)=20 + last_value(stts_duration) over (partition by stts_id o= rder by stts_offset desc) from table group by stts_id; ________________________________________ Venky Kandaswamy Principal Engineer, Adchemy Inc. 925-200-7124 ________________________________________ From: pgsql-sql-owner@postgresql.org on be= half of Gary Stainburn Sent: Thursday, July 25, 2013 10:57 AM To: pgsql-sql@postgresql.org Subject: Re: [SQL] value from max row in group by As usual, once I've asked the question, I find the answer myself. However, it *feels* like there should be a more efficient way. Can anyone comment or suggest a better method? timetable=3D> select stts_id, stts_offset+stts_duration as total_duration timetable-> from standard_trip_sections timetable-> where (stts_id, stts_offset) in timetable-> (select stts_id, max(stts_offset) from standard_trip_sections group by stts_id); stts_id | total_duration ---------+---------------- 1 | 01:35:00 2 | 01:35:00 3 | 01:08:00 4 | 01:38:00 5 | 01:03:00 6 | 01:06:00 (6 rows) timetable=3D> -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql