Sql介于多个列的子句之间

时间:2012-05-17 10:00:03

标签: sql

我尝试使用Between Clause实现查询,但是这个

存在一些问题

首先查询S_E1:

此查询返回我需要的确切数据,此处数据计数为43。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM' 
                           and '5/17/2012 12 :00 AM'
And ( S_E1 Between 10 And 100 )

S_E2的第二个查询:

此查询返回我需要的确切数据,此处数据计数为68。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E2 ,2 )as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
                           and '5/17/2012 12 :00 AM'
And ( S_E2 Between 10 And 100 )

问题:

但是当我在单个查询中将它们组合到查询中时,它会给出错误的数据。它给了我73行。我认为它应该给出111行

select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
                           and '5/17/2012 12 :00 AM'
And ( S_E2 Between 10 And 100) 
and (S_E1 Between 10 And 100  )

请在这里说明我错在哪里..

4 个答案:

答案 0 :(得分:1)

使用OR代替AND opeartor

SELECT RECORD_TIMESTAMP AS DateRecorded, 
       Round (S_E2, 2)  AS S_E2, 
       Round (S_E1, 2)  AS S_E1 
FROM   TBL_SENSORS 
WHERE  RECORD_TIMESTAMP BETWEEN '4/28/2012 12 :00 AM' AND '5/17/2012 12 :00 AM' 
       or ( S_E2 BETWEEN 10 AND 100 ) 
       or ( S_E1 BETWEEN 10 AND 100 ) 

在查询中,不保证您将获得111条记录,因为可能存在两种情况的共同记录。

条件1 - ( S_E2 BETWEEN 10 AND 100 )

条件2 - ( S_E1 BETWEEN 10 AND 100 )

或者

在两个查询中使用Union All来获取所有记录(111)。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E1, 0 as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM' and '5/17/2012 12 :00 AM'
And ( S_E1 Between 10 And 100 )

UNION ALL

select RECORD_TIMESTAMP as DateRecorded, 0 as S_E1, ROUND (S_E2 ,2 ) as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
and '5/17/2012 12 :00 AM'
And ( S_E2 Between 10 And 100 )

答案 1 :(得分:1)

我认为您应该在两个陈述之间建立联盟,或者您应该使用“OR”代替AND。

select RECORD_TIMESTAMP as DateRecorded,ROUND (S_E2 ,2 )as S_E2,ROUND (S_E1 ,2 )as S_E1 from TBL_SENSORS Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM' and '5/17/2012 12 :00 AM' AND (( S_E2 Between 10 And 100) OR(S_E1 Between 10 And 100 ))

答案 2 :(得分:1)

问题在于S_E2和S_E1的限制。

为了让他们按照预期的方式工作,您必须使用UNION ALL

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM' and '5/17/2012 12 :00 AM'
And ( S_E1 Between 10 And 100 )

UNION ALL

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E2 ,2 )as S_E
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
and '5/17/2012 12 :00 AM'
And ( S_E2 Between 10 And 100 )

或者您使用OR代替AND,如下所示:

select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
                           and '5/17/2012 12 :00 AM'
And ( (S_E2 Between 10 And 100) OR (S_E1 Between 10 And 100)  )

由于限制的唯一区别来自S_E1和S_E2列,如果您使用AND,则表示所有记录都必须遵守这两个限制。在分离的查询中,他们只尊重一个,所以你必须在主查询中这样做。

答案 3 :(得分:1)

通过放置额外的括号并将最后一个AND替换为OR

来更改布尔逻辑
select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between '4/28/2012 12 :00 AM'
                           and '5/17/2012 12 :00 AM'
And (( S_E2 Between 10 And 100) 
OR (S_E1 Between 10 And 100  ))