2

我尝试通过使用 Between 子句来实现查询,但这存在一些问题

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

4 回答 4

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' 
       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 )
于 2012-05-17T10:03:59.300 回答
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 ))
于 2012-05-17T10:04:03.980 回答
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 )

或者您使用 anOR而不是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,则表示所有记录都必须遵守这两个限制。在单独的查询中,它们只尊重一个,因此您必须在主查询中这样做。

于 2012-05-17T10:06:06.717 回答
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  ))
于 2012-05-17T10:09:42.330 回答