0

我有一个表,其中有许多冗余点,我想使用 (distinct) 选择不同的点并选择某些行的平均值(例如 rscp)。这里我们有一个例子:

| id |  point                   |   rscp    |   ci
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1  |  POINT(10.1192 36.8018)  |   10      |   701
| 2  |  POINT(10.1192 36.8018)  |   11      |   701
| 3  |  POINT(10.1192 36.8018)  |   12      |   701
| 4  |  POINT(10.4195 36.0017)  |   30      |   701
| 5  |  POINT(10.4195 36.0017)  |   44      |   701
| 6  |  POINT(10.4195 36.0017)  |   55      |   701
| 7  |  POINT(10.9197 36.3014)  |   20      |   701
| 8  |  POINT(10.9197 36.3014)  |   22      |   701
| 9  |  POINT(10.9197 36.3014)  |   25      |   701

我想要得到的是下表:(rscp_avg 是冗余点的 rscp 的平均值)

| id |  point                   |   rscp_avg    |   ci
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| *  |  POINT(10.1192 36.8018)  |   11          |   *
| *  |  POINT(10.4195 36.0017)  |   43          |   *
| *  |  POINT(10.9197 36.3014)  |   22.33       |   *

我试过这个,但它给了我一个错误的平均值!!!!

select distinct on(point)
id,st_astext(point),avg(rscp) as rscp_avg,ci
from mesures
group by id,point,ci;

谢谢你的帮助(^_^)

4

1 回答 1

0

哈姆杜拉!感谢上帝 !

我现在找到了解决方案:

select on distinct(point)
id,st_astext(point),rscp_avg,ci
from
(select id,point,avg(rscp) over w as rscp_avg,ci
from mesures
window w as (partition by point order by id desc)
) ss
order by point,id asc;

对我有帮助的网站是:

http://www.postgresql.org/docs/9.1/static/tutorial-window.html

http://www.w3resource.com/PostgreSQL/postgresql-avg-function.php

于 2013-03-27T23:22:23.297 回答