Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于sql的数据公式应用_Sql_Postgresql - Fatal编程技术网

基于sql的数据公式应用

基于sql的数据公式应用,sql,postgresql,Sql,Postgresql,我的数据是这样的 wavelength reflectance 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

我的数据是这样的

wavelength    reflectance
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
wavelength    reflectance
341.6         15
343.6         -7.61905
344.7         -32
346.3         -53.3333
347.9         -53.3333
349.5         -53.3333
351.1         150
352.6         160
354.2         140
波长值为w1,w2,w3,w4,。。。反射率值为r1,r2,r3,r4,。。。我想把r=(w2-w1)/(r2-r1)的公式应用于这个。计算值作为反射率341.6 15(ie)[r=(w2-w1)/(r2-r1)],343.6-7.61905(ie)[r=(w3-w2)/(r3-r2)]应用。我想要这样的输出

wavelength    reflectance
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
wavelength    reflectance
341.6         15
343.6         -7.61905
344.7         -32
346.3         -53.3333
347.9         -53.3333
349.5         -53.3333
351.1         150
352.6         160
354.2         140

在Postgresql中是否可能

可能我误解了您,因为结果不同,但可能这很有用:

select
wl,
(lead(wl,1) over (order by wl) - wl) /
(lead(ref,1) over (order by wl) - ref) as reflectance
from wa
order by wl
基本上 (波长[i+1]-波长[i])/(反射率[i+1]-反射率[i])

处理零除运算:

select
wl,
COALESCE(lead(wl,1) over (order by wl) - wl) /
nullif((lead(ref,1) over (order by wl) - ref),0),0) as reflectance
from wa
order by wl

用一个例子更详细地阐述它。w1、w2、r1、r2是什么?您如何决定执行哪两行计算?最好获取所有数据并使用php逐行排序。只是要小心不要超过数组限制。关系数据库中没有“第一行”或“第二行”这样的东西,除非您有可以对行进行排序的东西。您是否有这样一个列,可以定义结果的顺序,以便有类似“第一行”的内容?如果是,我认为这在窗口函数中是可行的。我的一些值被零除,因此它会给出这样的错误错误错误:被零除*********错误*********错误:被零除SQL状态:22012,我想使用case语句,如果任何被零除的东西将0放在那个位置,而其他人将值放在那个位置。如何在此查询中插入大小写