POSTGRESQL公式帮助通缉

时间:2014-03-19 04:41:51

标签: sql postgresql jsp

我的数据作为PostGreSQL的表格。它由1024行和2列组成。这是样本表

wv1           rv1
341.6         2.48
343.6         2.58
344.7         2.37
346.3         2.32
347.9         2.29
349.5         2.36
351.1         2.23
352.6         2.24
354.2         2.25
355.8         2.29
357.4         2.28
358.9         2.23

我从PostGreSQL使用

中选择了341到359之间wv1的数据
SELECT wv1 FROM grassland1 WHERE wv1 BETWEEN 341 AND 359

我在所选数据中应用此公式

The formula is **CRi=rv1i/(rv1START+K.(wv1i-wv1START))**
**K=(rv1END-rv1START)/(wv1END-wv1START)**
That is
rv1i - The current rv1 value
wv1i - Tha current wv1 value
rv1START - First value of rv1
rvlEND - last value of rv1
wv1START - First value of wv1
wvlEND - last value of wv1

步骤

分子的步骤

  • 当前rv1值为rvi

Denaminator的步骤

  • rv1的减法起始值和rv1结束值为 rv1END-rv1START(即,2.23-2.48)
  • 减去wv1的起始值和wv1的结束值为 wv1END-wv1START(即,358.9-341.6)
  • Divde(rv1END-rv1START)/(wv1END-wv1START)(即,(2.23-2.48)/(358.9-341.6)) 并将值命名为K
  • wv1i-wv1START的减法(即i = 341.6,343.6,344.7,...)和 将其与K值相乘为K.(wv1i-wv1START)
  • 使用K.(wv1i-wv1START)将rvl的起始值添加为 rv1START + K。(wv1i-wv1START)并将其命名为rv1

分子/ Denaminator

  • CRI = rv1i /(rv1START + K。(wv1i-wv1START))

我的输出将是这样的

 wv1           rv1
341.6         1
343.6         1.049496
344.7         0.973225
346.3         0.961825
347.9         0.958576
349.5         0.955264
351.1         0.951886
352.6         0.965084
354.2         0.979147
355.8         1.006683
357.4         1.012579
358.9         1

这是tis任务的PostGreSQL查询

DECLARE @rvlEND decimal, @rv1START decimal, @wv1END decimal, @wv1START decimal
    , @K decimal;

SET @wv1START = (SELECT MIN(wv1) FROM grassland1 
           WHERE wv1 BETWEEN 341 AND 359);
SET @wv1END = (SELECT MAX(wv1) FROM grassland1 
           WHERE wv1 BETWEEN 341 AND 359);
SET @rv1START = (SELECT rv1 FROM grassland1
           WHERE wv1 = @wv1START);
SET @rvlEND = (SELECT rv1 FROM grassland1 
           WHERE wv1 = @wv1END);

SET @K = (@rv1END - @rv1START)/(@wv1END - @wv1START);

现在已经计算了所有变量,以评估每一行的值。

SELECT t.wv1,
    t.rv1/(@rv1START + (@K*(t.wv1 - @wv1START))) as CRi
FROM grassland1 t
WHERE wv1 BETWEEN 341-359

我不知道如何查看收到的查询输出,如果不可能我怎么能在jsp中执行此操作...

1 个答案:

答案 0 :(得分:1)

以这种方式试试

SELECT wv1, rv1,
       rv1 / (rv1_first + ((rv1_last - rv1_first) / (wv1_last - wv1_first)) * (wv1 - wv1_first)) cri
  FROM
(
  SELECT wv1, rv1,
         FIRST_VALUE(wv1) OVER (ORDER BY wv1) wv1_first,
         FIRST_VALUE(wv1) OVER (ORDER BY wv1 DESC) wv1_last,
         FIRST_VALUE(rv1) OVER (ORDER BY wv1) rv1_first,
         FIRST_VALUE(rv1) OVER (ORDER BY wv1 DESC) rv1_last
    FROM grassland1
   WHERE wv1 BETWEEN 341 AND 359
) q
 ORDER BY wv1

输出:

|   WV1 |  RV1 |            CRI |
|-------|------|----------------|
| 341.6 | 2.48 |              1 |
| 343.6 | 2.58 | 1.052589378361 |
| 344.7 | 2.37 | 0.973225094353 |
| 346.3 | 2.32 | 0.961825109636 |
| 347.9 | 2.29 | 0.958576302354 |
| 349.5 | 2.36 | 0.997532312053 |
| 351.1 | 2.23 | 0.951886303634 |
| 352.6 | 2.24 | 0.965084424964 |
| 354.2 | 2.25 | 0.979146752528 |
| 355.8 | 2.29 | 1.006682929308 |
| 357.4 | 2.28 | 1.012578939262 |
| 358.9 | 2.23 |              1 |

这是 SQLFiddle 演示