2014-04-02 48 views
-1

我的數據是這樣的重值ON選擇

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 as 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 

我嘗試這個查詢1,它提供了一個單一的價值和操作上 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 

,而我想這query2它產生這麼多的值,在我的數據中,提供的所有三個區間只有3個值因此輸出將只有三個值 我的o嚴格的數據由1024行組成,所有三個區間都有25個值,所以我只想得到25個值的輸出。

+0

你嘗試過什麼?用您所做的任何嘗試更新您的問題。 –

+0

假設我沒有得到3個記錄的360-365的價值。那麼默認值是什麼? –

+0

[PostgreSQL上的多個公式]可能的重複(http://stackoverflow.com/questions/22782600/multiple-formula-on-postgresql) –

回答

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) 
+0

非常感謝你先生,你解決了我的問題.... –

1

選擇

2.5 *((R-I)/(R +(6 * I) - (7。5 * N)+1))AS TERA從

SELECT SUM (R)爲R,SUM(I)爲I,SUM(n)作爲N,ROW_FLAG

FROM (

選擇

CASE WHEN波長BETWEEN 345和350 THEN ELSE反射率0 END爲R,

CASE WHEN波長爲350〜355 THEN反射率ELSE 0 END爲I,

CASE WHEN波長BETWEEN 355和360然後通過(CASE反射率)以上(分區ELSE 0 END如N,

ROW_NUMBER(WHEN波長BETWEEN 345和350 THEN 1

WHEN波長爲350〜355 THEN 2

WHEN波長BETWEEN 355和360 THEN 3

ELSE 0 END)通過波長順序)AS ROW_FLAG

從YOUR_TABLE )筆

GROUP BY ROW_FLAG )筆

+0

@Gooner檢查你的鍵盤。它看起來像你有大寫鎖定按 –

+1

請不要在美國! –