postgresql数据的间隔平均值

时间:2014-02-28 06:51:12

标签: mysql sql postgresql

我的数据就像这样

wavelength    reflectance
341.6         2.48
343.6         2.58
347.6         4.51
351.1         8.51
359.2         9.56
362.2         11.2
364.2         25.3
365.3         58.2
366.6         58.2
368.9         24.2
373.6         28.2

我希望采用10个波长间隔,并且它们之间的反射间隔取平均值,然后输出将为I want output like this

wavelength    reflectnce
341.6         2.48
351.1         5.20
362.2         10.38
373.6         32.35

如果我使用此代码,则显示波长间隔很好。现在我想平均它们之间的反射间隔,我将把它放在下一行反射

    ;WITH CTE AS(
SELECT ROW_NUMBER() OVER(PARTITION BY CAST(wavelength AS INT)-CAST(wavelength AS INT)%10 ORDER BY wavelength) AS ROW_ID,wavelength,reflectance FROM Your_Table
)

SELECT wavelength,reflectance FROM CTE WHERE ROW_ID=1

如果有人知道,请帮助我......

1 个答案:

答案 0 :(得分:0)

我猜你看起来像这样 - > demo (click me please)

WITH CTE AS(
SELECT 
  ROW_NUMBER() OVER(
    PARTITION BY CAST(wavelength AS INT)
                 -CAST(wavelength AS INT)%10 
    ORDER BY wavelength) AS ROW_ID,
  wavelength,
  avg( reflectance ) OVER(
    PARTITION BY CAST(wavelength AS INT)
                 -CAST(wavelength AS INT)%10 
    ORDER BY wavelength
    rows BETWEEN 1 FOLLOWING
         and UNBOUNDED FOLLOWING) As reflectance 
  FROM 
  Your_Table
)
select wavelength, reflectance
from cte
where row_id = 1

这是此查询示例数据的结果:

| WAVELENGTH | REFLECTANCE |
|------------|-------------|
|      341.6 |       3.545 |
|      351.1 |        9.56 |
|      362.2 |      41.475 |
|      373.6 |      (null) |

计算出的平均值不包括第一个值,
例如,对于输入数据中的以下行:

wavelength    reflectance
341.6         2.48
343.6         2.58
347.6         4.51

avg =(2.58 + 4.51)/ 2 - >第一行被跳过