选择的多个价值观

时间:2014-04-02 06:23:38

标签: sql postgresql

我的数据就像这样

wavelength    reflectance
341.6         2.48
343.1         2.43
344.7         2.37
346.3         2.32
347.9         2.29
349.5         2.26
351.1         2.23
352.6         2.24
354.2         2.25
355.8         2.29
357.4         2.28
358.9         2.23
360.5         2.22
362.1         2.18
363.6         2.16
365.2         2.14

我想使用公式

  • 选择波长340-345之间的反射率为r
  • 选择波长350-355之间的反射率作为i
  • 选择波长360-365之间的反射率为n

公式

2.5*((r-i)/(r+(6*i)-(7.5*n)+1))
  • 输出值应命名为tera

预期产出

tera
-2.33333
........
........
........

公式

(r-(2*i)-n)/(r+(2*i)-n)
  • 输出应命名为tera

执行

  • 取所有三个选择的第一个值并应用公式并创建 输出
  • 获取所有三个中的第二,第三,第四个值,然后创建 输出为列

预期产出

tera
-0.89831
........
........
.........

查询1:

select
  2.5*((r-i)/(r+(6*i)-(7.5*n)+1)) as tera
from
  (
  select
    sum(case when wavelength between 340 and 345 then reflectance end) as r,
    sum(case when wavelength between 350 and 355 then reflectance end) as i,
    sum(case when wavelength between 360 and 365 then reflectance end) as n
  from
    test
  ) vars

我尝试了这个Query1,它在sum操作执行时给出了一个值 QUERY2:

SELECT
    2.5*((r-i)/(r+(6*i)-(7.5*n)+1)) AS TERA
    FROM
    (
    SELECT CASE WHEN wavelength BETWEEN 340 AND 345 THEN reflectance ELSE 0 END AS r,
    CASE WHEN wavelength BETWEEN 350 AND 355 THEN reflectance ELSE 0 END AS i,
    CASE WHEN wavelength BETWEEN 355 AND 360 THEN reflectance ELSE 0 END AS n
    FROM
    deodar
    ) T

虽然我正在尝试这个查询2它产生了很多值,在我的数据中,提供的所有三个区间只有3个值所以输出将只有三个值 我的原始数据由1024行组成,所有三个区间都有25个值,所以我想得到25个值的输出。

2 个答案:

答案 0 :(得分:1)

选择

2.5 *((r-i)/(r +(6 * i) - (7.5 * n)+1))AS TERA来自

选择   SUM(r)为r,SUM(i)为i,SUM(n)为n,ROW_FLAG

这    (

选择

当波长在345和350之间时波长反射ELSE 0 END AS r,

当波长在350和355之间波长时反射ELSE 0结束为i,

当波长在355和360之间波长时反射ELSE 0结束为n,

row_number()over(分区依据(当波长在345和350之间时为1)

当波长在350和355之间时为2

当波长在355和360之间时为3

ELSE 0 END)按波长排序)AS ROW_FLAG

来自YOUR_TABLE  )T

GROUP BY ROW_FLAG )T

答案 1 :(得分:1)

this

怎么样?
select
  2.5*((r-i)/(r+(6*i)-(7.5*n)+1)) as tera,
  (r-(2*i)-n)/(r+(2*i)-n) as tera2
from

    (select wavelength, reflectance as r, row_number() over(order by wavelength) as rn
     from test where wavelength between 340 and 345) t1
    join 
    (select wavelength, reflectance as i, row_number() over(order by wavelength) as rn
     from test where wavelength between 350 and 355) t2 using (rn)
    join
    (select wavelength, reflectance as n, row_number() over(order by wavelength) as rn
     from test where wavelength between 360 and 365) t3 using (rn)