我有一种感觉
(COMPLEX_EXPRESSION_N
代表长子查询)
select
ID_Operation,
FirstCheck = CASE WHEN (COMPLEX_EXPRESSION_1)= 0 then 0 else 1 end,
SecondCheck = CASE WHEN (COMPLEX_EXPRESSION_2)= 0 then 0 else 1 end,
ThirdCheck = CASE WHEN (COMPLEX_EXPRESSION_3)= 0 then 0 else 1 end,
AllChecksOk = Case WHEN
(FirstCheck + SecondCheck + Third CHeck = 3)
Then 'OK' Else 'No' End
from
AllOperationsTable
是否可以像在AllChecksOk行中那样使用FirstCheck、SecondCheck、ThirdCheck?
我不关心性能,这是每天手动运行一次的非常少的记录,我只是想避免创建视图,表或临时表,并保持在一个单一的选择语句。
作为一种替代方法,我可以这样做,但这会使查询的可读性降低(因为我需要为每个复杂的表达式编写两次):
select
ID_Operation,
FirstCheck = CASE WHEN (COMPLEX_EXPRESSION_1)= 0 then 0 else 1 end,
SecondCheck = CASE WHEN (COMPLEX_EXPRESSION_2)= 0 then 0 else 1 end,
ThirdCheck = CASE WHEN (COMPLEX_EXPRESSION_3)= 0 then 0 else 1 end,
AllChecksOk = Case WHEN
(COMPLEX_EXPRESSION_1+ COMPLEX_EXPRESSION_2+
COMPLEX_EXPRESSION_3CHeck = 3) Then 'OK' Else 'No' End
from
AllOperationsTable
2条答案
按热度按时间m1m5dgzv1#
您不能引用
select
中的列别名,但可以使用CTE,如下所示。您还可以使用
CROSS APPLY
定义3个列别名,然后在主SELECT
列表as in this example中引用它们。3vpjnl9f2#
下面是一个派生表解决方案