我想实现以下目标:
SELECT (CASE WHEN ...) AS FieldA, FieldA + 20 AS FieldB FROM Tbl
假设我用“ …”代替了一个冗长而复杂的 CASE 语句,那么在选择并使用别名时, 我不想重复 它。FieldB``FieldA
FieldB``FieldA
请注意,这将返回多行,因此在我的情况下,语句外的DECLARE/是不好的。SET``SELECT
DECLARE
SET``SELECT
一种工作方式是使用子查询:
SELECT FieldA, FieldA + 20 AS FieldB FROM ( SELECT (CASE WHEN ...) AS FieldA FROM Tbl ) t
为了提高可读性,您还可以使用CTE:
CTE
WITH t AS ( SELECT (CASE WHEN ...) AS FieldA FROM Tbl ) SELECT FieldA, FieldA + 20 AS FieldB FROM t