我的数据是这样
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我想用这个公式
公式是
2.5*((r-i)/(r+(6*i)-(7.5*n)+1))预期产出
tera
-2.33333公式是
(r-(2*i)-n)/(r+(2*i)-n)预期产出
tera
-0.89831对独立公式的单独查询。在PostgreSQL中可以应用这样的公式吗?像这样..。
发布于 2014-04-01 10:31:33
SQLFiddle
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
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对于多个表,可以使用以下内容:
select
table_name,
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
table_name,
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
(
select 'table 1' as table_name, * from test
union all
select 'table 2', * from test
union all
select 'table 3', * from test
union all
select 'table 4', * from test
) as all_tables
group by
table_name
) varsSQLFiddle
您只需将表名修改为实际名称,并根据需要重复多次union all。
发布于 2014-04-01 10:27:41
我有个建议给你。我已经将公式添加到相同的查询中。如果需要在单独的语句中使用它们,那么只需将公式转到两个查询中。
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
SUM(CASE WHEN tbl.wavelength BETWEEN 340 AND 345 THEN tbl.reflectance ELSE 0 END) AS r,
SUM(CASE WHEN tbl.wavelength BETWEEN 350 AND 355 THEN tbl.reflectance ELSE 0 END) AS i,
SUM(CASE WHEN tbl.wavelength BETWEEN 360 AND 365 THEN tbl.reflectance ELSE 0 END) AS n
FROM
table1 as tbl
) as outertbl输出将是
tera tera2
-2.33333333333335 -0.898305084745763https://stackoverflow.com/questions/22782600
复制相似问题