[SQL] value from max row in group by

2013-07-25 Thread Gary Stainburn
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

Re: [SQL] value from max row in group by

2013-07-25 Thread Gary Stainburn
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= select stts_id, stts_offset+stts_duration as total_duration timetable- from standard_trip_sections

Re: [SQL] value from max row in group by

2013-07-25 Thread bricklen
On Thu, Jul 25, 2013 at 10:45 AM, Gary Stainburn gary.stainb...@ringways.co.uk wrote: 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

Re: [SQL] value from max row in group by

2013-07-25 Thread Venky Kandaswamy
You can use Postgres WINDOW functions for this in several different ways. For example, one way of doing it: select stts_id, last_value(stts_offset) over (partition by stts_id order by stts_offset desc) + last_value(stts_duration) over (partition by stts_id order by

Re: [SQL] value from max row in group by

2013-07-25 Thread Marc Mamin
Von: pgsql-sql-ow...@postgresql.org [pgsql-sql-ow...@postgresql.org]quot; im Auftrag von quot;Venky Kandaswamy [ve...@adchemy.com] You can use Postgres WINDOW functions for this in several different ways. For example, one way of doing it: select