我正在编写一个查询来定义Sybase Database
上的视图。请考虑以下示例
SELECT a, (b+c+d-e) AS derived_1, (b+c+d-e)+2 AS derived_2, (b+c+d-e)+4 as derived_3
FROM TABLENAME
您可以看到我使用某些逻辑(b + c + d-e)计算值,为不同的派生列应用更多逻辑和返回值。有没有什么方法可以写一次(b + c + d-e)?
像
这样的东西SELECT a, (b+c+d-e) AS derived_1, derived_1+2 AS derived_2, derived_1+4 as derived_3
FROM TABLENAME
答案 0 :(得分:1)
将计算包装在子查询中,
SELECT a,
derived_1 + 2 AS derived_2,
derived_1 + 4 AS derived_3
FROM
(
SELECT a, (b+c+d-e) AS derived_1
FROM TABLENAME
) x