对不同的SQL别名使用相同的计算

时间:2013-03-28 10:31:30

标签: sql alias

所以我有两个使用相同计算的列别名:

,case when bi.PolicyFeeFactor = 0 then 0 else CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2)) end as UNIT_POLFEE_Y

,case when bi.PolicyFeeFactor = 0 then 0 else CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2)) end as UNIT_PUPFEE_Y

我正在寻找一种方法,我只需编写一次计算并从该语句创建两个列别名,例如:

,case when bi.PolicyFeeFactor = 0 then 0 else CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2)) end as UNIT_PUPFEE_Y, UNIT_POLFEE_Y

2 个答案:

答案 0 :(得分:0)

将其包装在子查询中,ex

SELECT Result AS UNIT_PUPFEE_Y, 
       Result AS UNIT_POLFEE_Y
FROM
    (
        SELECT CASE .... END AS Result
        FROM   tableName
    ) s

答案 1 :(得分:0)

你可以这样写,

SELECT *,UNIT_PULFEE_Y AS UNIT_PUPFEE_Y FROM
(SELECT *
,case when bi.PolicyFeeFactor = 0 then 0 else CAST(ROUND(nb.AnnualPolicyFee *              bi.PolicyFeeFactor,2)AS DECIMAL(6,2)) end as UNIT_PULFEE_Y
FROM Table )A