1

试图WHERE BETWEEN用 a 做 a CASE WHEN

SET @Qtr1 = datepart(MONTH,@dt1)
select 
SUM(QtyInvoiced) as QtrCase
,CustName
,Town
,Rep
from SalesSumNew 
where
FinMonth between
case when @Qtr1 between '1' and '3' THEN '10' and '12'
,case when @Qtr1 between '4' and '6' THEN '1' and '3'
,case when @Qtr1 between '7' and '9' THEN '4' and '6'
,case when @Qtr1 between '10' and '12' THEN '7' and '9'
end
group by CustName, Town, Rep
order by Town

结果错误:

消息 156,级别 15,状态 1,第 57 行
关键字“和”附近的语法不正确。

4

1 回答 1

3

You can't do that.

Try calculating the range using maths and mod : something like...

where FinMonth between 
((((2+@qtr1)/3)+2)*3)%12+1,
and
((((2+@qtr1)/3)+2)*3+2)%12+1

And if your FinMonth is really a string field, then '10','11','12' are all between '1' and '3'

于 2012-08-24T09:26:01.530 回答