我正在尝试在CASE
子句中编写WHERE
语句,类似于此。
我的问题是,最后我要说ELSE CalendarQuarter IN (@Q1,@Q2,@Q3,@Q4)
。因此,如果没有满足这些条件,请执行其他操作。我无法弄清楚如何添加此ELSE
语句。
AND CalendarQuarter in
(
/*March, June, Sep 2010 Metcard*/
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6,9) THEN @Q1 END),
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6) THEN @Q2 END),
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3) THEN @Q3 END),
/*March, June, Sep 2010 Metcard/Myki*/
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9,6,3) THEN @Q4 END),
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9,6) THEN @Q3 END),
(CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9) THEN @Q2 END),
/*March, June, Sep 2013 Metcard/Myki*/
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3,6,9) THEN @Q1 END),
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3,6) THEN @Q2 END),
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3) THEN @Q3 END),
/*March, June, Sep 2013 Myki*/
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9,6,3) THEN @Q4 END),
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9,6) THEN @Q3 END),
(CASE WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9) THEN @Q2 END),
/*ELSE....IN (@Q1,@Q2,@Q3,@Q4)*/
)
答案 0 :(得分:2)
我没有足够的声誉来为您的帖子添加评论,但听起来您想添加另一个案例陈述。 ELSE不应该有任何条件,它用于捕获与它附加的case语句中的任何条件不匹配的所有条件。
e.g。 列foo可以包含1,2,3或4,我们希望将列分别设置为a,b,c或c。我们有以下案例陈述
CASE
WHEN foo = 1
THEN 'a'
WHEN foo = 2
THEN 'b'
ELSE 'c'
END
我们不需要为3或4添加一个案例,因为它们与1或2不匹配,因此它们将由else处理,将它们设置为c。
如果您尝试向else语句添加另一个条件,则可以在else中使用另一个case语句。在这种情况下,我们希望将foo 3或4的值转换为x或y。
CASE
WHEN foo = 1
THEN 'a'
WHEN foo = 2
THEN 'b'
ELSE CASE
WHEN foo = 3
THEN 'x'
WHEN foo = 4
THEN 'y'
ELSE NULL
END
END
当然,您可以使用您想要的任何值,您不需要在条件中评估单个列。
编辑:我使用你的case语句创建了一个例子。它在ELSE中有第二个case语句,允许你向else添加条件。
AND CalendarQuarter IN
(
CASE
/*March, June, Sep 2010 Metcard*/
WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6,9)
THEN @Q1
WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6)
THEN @Q2
WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3)
THEN @Q3
/*March, June, Sep 2013 Metcard/Myki*/
CASE WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9,6,3)
THEN @Q4
WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9,6)
THEN @Q3
WHEN DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (9)
THEN @Q2
/*March, June, Sep 2013 Metcard/Myki*/
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3,6,9)
THEN @Q1
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3,6)
THEN @Q2
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'metcard/myki'
AND DATEPART(mm,@year_end) IN (3)
THEN @Q3
/*March, June, Sep 2013 Myki*/
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9,6,3)
THEN @Q4
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9,6)
THEN @Q3
WHEN DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9)
THEN @Q2
ELSE CASE
WHEN [YOUR CONDITION HERE] IN (@Q1,@Q2,@Q3,@Q4)
THEN [SOME RESULT]
END
END
)
上述声明将在条件满足后立即返回。另一种方法是使用原始case语句,但不使用else,而是使用上述条件的倒数。 e.g。
CASE
WHEN DATEPART(yy,@year_end) = 2013
THEN CASE
WHEN @ticketing_system = 'myki'
THEN CASE
WHEN DATEPART(mm,@year_end) NOT IN (9,6,3)
THEN (@Q1,@Q2,@Q3,@Q4)
END
WHEN @ticketing_system = 'metcard/myki'
THEN CASE
WHEN DATEPART(mm,@year_end) NOT IN (3,6,9)
THEN (@Q1,@Q2,@Q3,@Q4)
END
END
WHEN DATEPART(yy,@year_end) = 2010
THEN CASE
WHEN @ticketing_system = 'metcard/myki'
THEN CASE
WHEN DATEPART(mm,@year_end) NOT IN (9,6,3)
THEN (@Q1,@Q2,@Q3,@Q4)
END
WHEN @ticketing_system = 'metcard'
THEN CASE
WHEN DATEPART(mm,@year_end) NOT IN (3,6,9)
THEN (@Q1,@Q2,@Q3,@Q4)
END
END
END
答案 1 :(得分:0)
我建议你避免使用case条款并尝试使用:
WHERE (DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6,9)
AND CalendarQuarter IN (@Q1))
OR
(DATEPART(yy,@year_end) = 2010
AND @ticketing_system = 'metcard'
AND DATEPART(mm,@year_end) IN (3,6)
AND CalendarQuarter IN (@Q2))
..................................
OR
(DATEPART(yy,@year_end) = 2013
AND @ticketing_system = 'myki'
AND DATEPART(mm,@year_end) IN (9)
AND CalendarQuarter IN(@Q2) )
OR
(CalendarQuarter IN (@Q1,@Q2,@Q3,@Q4))