0

我正在编写一个查询来定义一个视图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+de)计算了一个值,应用了更多逻辑并为不同的派生列返回值。有什么办法可以让我只写一次 (b+c+de) 吗?

就像是

SELECT a, (b+c+d-e) AS derived_1, derived_1+2 AS derived_2, derived_1+4 as derived_3
FROM TABLENAME
4

1 回答 1

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
于 2013-04-01T09:43:29.117 回答