【问题标题】:One Query output execution in another query一个查询输出在另一个查询中执行
【发布时间】:2023-03-18 15:57:02
【问题描述】:

说明

  • 选择最大反射率值作为最大值,最小反射率值作为最小值,其中 波长在 650 和 800 之间,并应用公式 (max+min)/2+min 作为辐射度
  • 选择波长接近 700 的反射率为 r700 和
  • 选择波长接近740的反射率为r740

    应用公式

    700+(辐射-r700)/(r740-r700)*40

输出值作为 radtera,我想显示辐射值和 radtera 值作为输出

我试过这个查询,它在辐射度中显示了很多值,并且在 radtera 中显示了许多空值,但我只想显示一个辐射度值和一个 radtera 值

SELECT radiance, (700+(radiance-r700))/((r740-r700)*40) as radtera
FROM (
  SELECT (MAX(reflectance)+MIN(reflectance))/2+MIN(reflectance) as radiance, 
         case when wavelength=700 then reflectance end as r700,
         case when wavelength=740 then reflectance end as r740
  FROM table_name
  WHERE wavelength between 650 and 800
  GROUP BY wavelength,reflectance
) AS SE_23693370 

如果我删除

按波长、反射率分组

这个来自查询,它显示错误

这里是SQL fiddle

我检查了它,我不知道它是如何显示多个值而不是一个值显示的.. 请任何人帮助我纠正错误...

我尝试在 SQL fiddle 中使用 case 语句。

select reip,700+(reip-r45)/(r72-r45)*40 as reipw
from ( 
  select (mx+mn))/2+mn as reip 
from ( 
  select case max(tert) as mx, 
  case min(tert) as mn 
  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)bar
)as SE_23693370

它显示 *ERROR: subquery in FROM must have an alias: select reip,700+(reip-r45)/(r72-r45)40 as reipw from (select (mx+mn))/2+ mn as reip from ( select case max(tert) as mx, case min(tert) as mn 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 介于 43 和 79 之间) ) 为 SE_23693370

【问题讨论】:

  • 你想让 StackOverflow 帮你做作业吗?

标签: mysql sql postgresql


【解决方案1】:

您可以使用ROW_NUMBER 函数来做到这一点。它在SQL ServerOraclePostgreSQL 等中可用。

select radiance, r700, r740,
  (700+(radiance-r700))/((r740-r700)*40) as radtera
from
(select t.reflectance as radiance, 
  t700.wavelength as r700, t740.wavelength as r740
from
  (select max(reflectance) as reflectance
  from table_name) t,
    (SELECT reflectance, wavelength,
      row_number() over (order by abs(wavelength-700)) as rn700
    FROM table_name
    WHERE wavelength between 650 and 800) as t700,
   (SELECT reflectance,wavelength,
      row_number() over (order by abs(wavelength-740)) as rn740
    FROM table_name
    WHERE wavelength between 650 and 800) as t740
  where t700.rn700=1 and t740.rn740=1) as di

Fiddle

【讨论】:

  • 只是为了完成图片:窗口函数 (row_number()) 在更多 DBMS 中可用,而不仅仅是您列出的三个。其中包括:DB2、Teradata、Sybase、Informix 和 Vertica。
猜你喜欢
  • 2016-08-09
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多