2014-05-06 39 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