要通过sql查询在值接近10,20,30 ...的列中查找这些值

时间:2015-01-12 09:26:49

标签: sql-server where

我有一个sql查询,它从三个表中检索值。当某个列值为0时,将显示相应的列值,否则将在该列中输入0。现在 I want to retrieve only those values from beam_current where values are near by to 10,20,30,40 ..till 220 。我目前的SQL查询是: -

select b.LOGTIME, b.beam_current, b.beam_energy,
  case when a.st1_vs1_bag1_onoff=0 and a.logtime=c.logtime then c.st1_vs1_bag1_rb ELSE 0 END as st1_vs1_bag1_rb ,
  CASE when a.st1_vs1_bag2_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag2_rb else '0' END as st1_vs1_bag2_rb ,
  CASE when a.st1_vs1_bag3_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag3_rb else '0' END as st1_vs1_bag3_rb ,
  CASE when a.st1_vs1_bag4_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag4_rb else '0' END as st1_vs1_bag4_rb ,
  CASE when a.st1_vs1_bag5_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag5_rb else '0' END as st1_vs1_bag5_rb ,
  CASE when a.st1_vs1_bag6_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag6_rb else '0' END as st1_vs1_bag6_rb ,
  CASE when a.st1_vs1_bag7_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag7_rb else '0' END as st1_vs1_bag7_rb ,
  CASE when a.st1_vs1_bag8_onoff='0' and a.logtime=c.logtime then c.st1_vs1_bag8_rb else '0' END as st1_vs1_bag8_rb 

 from INDUS2_BDS.dbo.DCCT b  INNER JOIN (main_vacuum_analog c inner join main_vacuum_status a on c.logtime=a.logtime) ON a.LOGTIME = b.LOGTIME 
--and (b.beam_current between 4.99 and 5.01
--or b.beam_current in (9.99, 10.00,10.01))
and b.logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00'

EDIT 1

Case语句将检查是否有st1_vs1_bag2_onoff = 0然后它将显示st1_vs1_bag2_rb的值,否则0将显示在相应的值 ,我们还检索所有beam_current值但我只希望beam_current中的那些值接近10,20,30 ......直到220 Is there any way to specify such a near by condition in sql query in sql server2012

1 个答案:

答案 0 :(得分:2)

我猜测,"附近"表示beam_current的值接近10,20,30,...,220。"

有多近?下面的公式给你NN + - 0.01。 %Modulo

and (b.beam_current % 10.0 <= 0.01 OR b.beam_current % 10.0 >= 9.99)
and (b.beam_current >= 9.99) -- to set the lower limit of 9.99
and (b.beam_current <= 220.01) -- to set the upper limit of 220.01

将这些内容添加到WHERE的{​​{1}}条款中。

修改

现在我们知道SELECT属于beam_current类型,我们只需将real类型转换为numeric

real

在这种情况下,精度9应该足够了,因为值小于220.01,我们只对小数点后两位感兴趣。