在单个表上的多个时间间隔上应用乘法

时间:2014-02-19 09:11:04

标签: mysql sql postgresql-9.1

我的数据由1024行和2列组成。它的结构类似于

  

enter image description here

我想根据第一列从单个表中选择三个区间。在选定的时间间隔内,第二列值应乘以值

SELECT * FROM del 
   WHERE wavelength BETWEEN 341 AND 348 
         OR wavelength BETWEEN 551 AND 664  
         OR wavelength BETWEEN 998 AND 1021

在这一个中,使用此查询和

选择间隔
  • 第一个间隔第二列值乘以0.25
  • 第二个间隔第二列乘以0.5
  • 第三个间隔第二列应乘以0.25。

在乘法后,我想在第二列中显示波长和相乘的反射率值

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT wavelength, 
       CASE WHEN wavelength BETWEEN 551 AND 664 THEN reflectance * .5
            ELSE  reflectance * .25 
       END AS 'REFLECTANCE'
FROM del 
WHERE wavelength BETWEEN 341 AND 348 
   OR wavelength BETWEEN 551 AND 664  
   OR wavelength BETWEEN 998 AND 1021

或常见的选择:

SELECT wavelength, 
       CASE WHEN (wavelength BETWEEN 341 AND 348) 
              OR (wavelength BETWEEN 998 AND 1021) THEN reflectance * .25 
            WHEN wavelength BETWEEN 551 AND 664 THEN reflectance * .5
            ELSE 1 ---(PUT YOUR DEFAULT) 
       END AS 'REFLECTANCE'
FROM del

答案 1 :(得分:1)

第一个和第三个范围乘以0.25,第二个范围乘以0.5

您可以使用以下查询。

SELECT wavelength, 
       ( reflect * 0.25 ) AS reflect 
FROM   wavelength 
WHERE  ( wavelength BETWEEN 341 AND 348 ) 
        OR ( wavelength BETWEEN 998 AND 1021 ) 
UNION ALL 
SELECT wavelength, 
       ( reflect * 0.5 ) AS reflect 
FROM   wavelength 
WHERE  wavelength BETWEEN 551 AND 664; 

如果要将所有三个乘以不同的值,则可以使用

SELECT wavelength, 
       ( reflect * 0.25 ) AS reflect 
FROM   wavelength 
WHERE  wavelength BETWEEN 341 AND 348 
       OR wavelength BETWEEN 998 AND 1021 
UNION ALL 
SELECT wavelength, 
       ( reflect * 0.5 ) AS reflect 
FROM   wavelength 
WHERE  wavelength BETWEEN 551 AND 664 
UNION ALL 
SELECT wavelength, 
       ( reflect * 0.25 ) AS reflect 
FROM   wavelength 
WHERE  wavelength BETWEEN 998 AND 1021 

演示http://sqlfiddle.com/#!2/2c41b/10