Re: aggregate and order by - Mailing list pgsql-general

From Gregory Stark
Subject Re: aggregate and order by
Date
Msg-id 87abonr1kf.fsf@oxford.xeocode.com
Whole thread Raw
In response to aggregate and order by  ("Matthew Dennis" <mdennis@merfer.net>)
Responses Replication Monitoring
List pgsql-general
"Matthew Dennis" <mdennis@merfer.net> writes:

> So, my question is if I can have PostgreSQL honor order by clauses such as:
>
> select trip_id, avg_vel(position, pos_time)
>   from (select position, pos_time, trip_id from data order by pos_time) sorted_data
>
> Would this in fact guarantee that the rows are passed into the aggregate in
> the order specified?

Yes. AFAIK this isn't covered by the spec but it works in Postgres and we know
there are people depending on it so we wouldn't break it without a big notice
and presumably some replacement.

> Other suggestions/discussions/questions/etc are welcome.

Good luck, this looks pretty painful to get right. Keep in mind you have to
keep all your state in the state data. If you keep a temporary variable
outside that data then your function won't work if it's called twice in the
same query like "select avg_vel(position, pos_time), avg_vel(position2,
pos_time2) from ..."

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Ask me about EnterpriseDB's 24x7 Postgres support!

pgsql-general by date:

Previous
From: Ted Byers
Date:
Subject: SQL design pattern for a delta trigger?
Next
From: Glyn Astill
Date:
Subject: Replication Monitoring