我想在日期范围内获取记录。他的查询应仅过滤,其中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
答案 0 :(得分: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