如何将 'case when' 结果应用于 SQL 2008 查询中的 where 条件

How to apply 'case when' result to the where condition in SQL 2008 query

我正在查询中使用 'case when' 语句并进行一些实验。我试图搜索这个问题,但我无法用语言表达它。

我的查询问题示例:

Select (a really long case when statement) as result from tablename where result=1 

有什么方法可以将 'result' 也放入 'where' 条件吗?

是的。最简单的方法是使用 CROSS APPLY (VALUES...

SELECT v.MyValue
FROM tablename
CROSS APPLY (VALUES (complex_expression_here) ) v(MyValue)
WHERE v.MyValue = 1

您也可以将它放在派生的 table 中,但我发现这样更冗长

SELECT *
FROM (
    SELECT complex_expression_here AS MyValue
    FROM tablename
) t
WHERE v.MyValue = 1