Re: Query sought with windowing function to weed out dense points

Поиск
Список
Период
Сортировка
От Gianni Ciolli
Тема Re: Query sought with windowing function to weed out dense points
Дата
Msg-id 20110217001654.GA28780@albo.gi.lan
обсуждение исходный текст
Ответ на Query sought with windowing function to weed out dense points  (Stefan Keller <sfkeller@gmail.com>)
Список pgsql-general
Hi,

On Thu, Feb 17, 2011 at 12:14:28AM +0100, Stefan Keller wrote:

> SELECT ST_AsText(geometry), name as label
> FROM
>   peaks t1
> WHERE
>   t1.id = (
>     SELECT id
>     FROM (
>       SELECT ST_SnapToGrid(geometry, 5) as geometry, elevation, id
>       FROM peaks
>     ) t2
>     WHERE ST_Equals(ST_SnapToGrid(t1.geometry, 5), t2.geometry)
>     ORDER BY elevation DESC
>     LIMIT 1
>   )

> I think there could be perhaps an even more elegant solution with the
> new windowing functions! My idea is to partition peaks around a grid
> and chose the one with max(elevation).

You might eliminate the correlated subquery, like in:

  SELECT *
  FROM (
    SELECT ST_AsText(geometry)
    , name as label
    , rank() OVER (
      PARTITION BY ST_Equals(ST_SnapToGrid(geometry, 5)
      ORDER BY elevation DESC)
    FROM
      peaks
  ) x
  WHERE rank = 1;
  -- query not tested as I don't have postgis available

which "feels" more elegant; but you still need a subquery, as window
functions are not allowed in the WHERE clause.

Cheers,
Dr. Gianni Ciolli - 2ndQuadrant Italia
PostgreSQL Training, Services and Support
gianni.ciolli@2ndquadrant.it | www.2ndquadrant.it


В списке pgsql-general по дате отправления:

Предыдущее
От: Stefan Keller
Дата:
Сообщение: Query sought with windowing function to weed out dense points
Следующее
От: Noah Misch
Дата:
Сообщение: Re: why does the toast table exist?