2014-05-06 42 views
0

我想在日期範圍內獲取記錄。他查詢應該只過濾,其中colA或ColB或ColC中的值大於0並且不等於null。現在,如果我嘗試這個查詢,我會得到所有列中2012,2011和0值的範圍。任何幫助表示讚賞。謝謝。我使用SQL 2008 查詢:基於所有列中的值進行篩選

select ColA, ColC, ColB 
from tableA join 
    tableB on tableA.id = TableB.id 
where ColA > 0 
    or ColB > 0 
    or ColC > 0 
    and tableB .Collection Date between 01-01-2013 and 12-31-2013 

數據:

date range colA colB ColB 
2/4/2013 402 88  null 
5/4/2012 null 501 522 
12/6/2013 110 550 null 
12/20/2013 85 null 101 
12/8/2013 null null null 
6/17/2012 852 225 null 
3/22/2013 null null null 
3/2/2013 null null null 

輸出應爲─

date range colA colB ColB 
2/4/2013 402  88  null 
12/6/2013 110  550  null 
12/20/2013 85  null 101 

回答

1

嘗試使用括號:

select ColA, ColC, ColB 
from tableA join 
    tableB on tableA.id = TableB.id 
where (ColA > 0 or ColB > 0 or ColC > 0) 
    and tableB.[Collection Date] between 01-01-2013 and 12-31-2013 

使用COALESCE

select ColA, ColC, ColB 
from tableA join 
    tableB on tableA.id = TableB.id 
where COALESCE(ColA,ColB,ColC) IS NOT NULL 
    and tableB.[Collection Date] between 01-01-2013 and 12-31-2013 
+0

唉......我錯過了括號。謝謝@Raging Bull – user3502587