将两个查询组合为一个查询

时间:2014-05-21 04:13:02

标签: sql postgresql

描述

  • 选择最大叔值作为最大和最小叔值作为min where iner在43和79之间,并应用公式(max + min)/ 2 + min作为reip
  • 选择更接近45的叔为r45和
  • 选择更靠近72的叔为r72 Apply Formula

700+(reip-r45)/(r72-r45)*40

输出值为repw 我想显示reip值和repw值作为输出

我在PostGreSQL中尝试过它

Select (MAX(tert)+MIN(tert))/2+MIN(tert) as reip from table_name where iner between 43 and 79

这是有效的,但我不知道在这个公式中应用700+(reip-r45)/(r72-r40)* 40中的这个reip值以及如何将输出值显示为reip和repw

我试过这个查询它不起作用..

select reip, 700+(reip-r45)/(r72-r45)*40 as reipw
from ( 
  select (MAX(tert)+MIN(tert))/2+MIN(tert) as reip, tert where iner=44.5 as r45, tert where iner=71.9 as r72
  from  table_name
  where iner between 650 and 800
) as SE_23693370 

如何将任务作为单个查询执行?有人引导我......

1 个答案:

答案 0 :(得分:1)

更改您的查询,如下所示

select reip, 
(700+(reip-r45))/((r72-r45)*40) as reipw
from ( 
  select (MAX(tert)+MIN(tert))/2+MIN(tert) as reip, 
  case when iner=44.5 then tert end as r45, 
case when iner=72.1 then tert end as r72
  from  table_name
  where iner between 43 and 79
  group by iner,tert
) as SE_23693370