2012-05-17 44 views
2

我嘗試通過分句間來實現查詢,但對於S_E1在此SQL子句之間的多個列

第一查詢的一些問題:

該查詢返回,我需要精確的數據,在這裏數據數是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) 

問題:

但是,當我在一個查詢中組合使用這些以查詢則給出錯誤data.It給了我73個rows.I覺得應該給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 ) 

請給一些想法,我錯了我在這裏..

回答

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) 
+0

。 –

+0

+1現在更喜歡它:) –

+0

@Romil謝謝你的答覆。你說得對,我現在正在或現在工作。但問題是現在SE_1或S_E2也顯示-ve values.There計數是73.數據是如此珍貴這裏是因爲它處理實時傳感器。你有任何解決方案或想法。 – Sunny

1

我認爲你應該在兩個語句之間進行聯合,或者你應該使用「或」而不是「與」。

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)) 
+0

+1你是對的! :) –

+1

非常感謝你 –

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,則表示所有記錄必須同時尊重限制。在分離的查詢中,他們只尊重一個,所以你必須在主查詢中這樣做。

1

更改布爾邏輯,通過將多餘的括號和替換最後一個爲OR他沒有使用UNION任何地方

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 )) 
+0

+1這就是解決方案:) –