SQL Server如何在同一个查询中使用别名?

时间:2018-03-02 11:22:49

标签: sql sql-server sql-server-2017

我会以这种方式写我的查询,但

SELECT 
     TempInventoryComparisonReal.Quantity - ISNULL([TempInventoryComparisonVirtual].[Quantity], 0)  QuantityDifference
     ,IIF((QuantityDifference > 0), QuantityDifference, 0) QuantityDifferencePositive
FROM [TempInventoryComparisonReal]
LEFT JOIN [TempInventoryComparisonVirtual] 
ON ([TempInventoryComparisonReal].ItemsCoresId = [TempInventoryComparisonVirtual].ItemsCoresId 
AND [TempInventoryComparisonReal].Dimensions1Id=[TempInventoryComparisonVirtual].Dimensions1Id
AND [TempInventoryComparisonReal].Dimensions2Id=[TempInventoryComparisonVirtual].Dimensions2Id
AND [TempInventoryComparisonReal].SerialNumber=[TempInventoryComparisonVirtual].SerialNumber)

但是sql server无法看到别名

Msg 207, Level 16, State 1, Procedure InventoryComparisonRealView, Line 5 [Batch Start Line 37]
Invalid column name 'QuantityDifference'.
Msg 207, Level 16, State 1, Procedure InventoryComparisonRealView, Line 5 [Batch Start Line 37]
Invalid column name 'QuantityDifference'. 

我必须重复公式,查询变得不容易阅读!

,ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) QuantityVirtual
,IIF((ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) - TempInventoryComparisonReal.Quantity)>0, (ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) - TempInventoryComparisonReal.Quantity), 0) QuantityDifferencePositive
,IIF((ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) - TempInventoryComparisonReal.Quantity)<0,(ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) - TempInventoryComparisonReal.Quantity) , 0) QuantityDifferenceNegative

4 个答案:

答案 0 :(得分:2)

当我真的想要使用别名来保持其可读性时,我倾向于将其放入子查询中,这样您就可以在select中使用别名。

答案 1 :(得分:2)

基本上A.van Esveld说:

SELECT QuantityDifference
    , IIF((QuantityDifference > 0), QuantityDifference, 0) QuantityDifferencePositive
FROM (
    select TempInventoryComparisonReal.Quantity - ISNULL([TempInventoryComparisonVirtual].[Quantity], 0) QuantityDifference
    from [TempInventoryComparisonReal]
    left join [TempInventoryComparisonVirtual] on (
            [TempInventoryComparisonReal].ItemsCoresId = [TempInventoryComparisonVirtual].ItemsCoresId
            and [TempInventoryComparisonReal].Dimensions1Id = [TempInventoryComparisonVirtual].Dimensions1Id
            and [TempInventoryComparisonReal].Dimensions2Id = [TempInventoryComparisonVirtual].Dimensions2Id
            and [TempInventoryComparisonReal].SerialNumber = [TempInventoryComparisonVirtual].SerialNumber
            )
) R

答案 2 :(得分:1)

尝试使用子查询。

    SELECT  IIF((QuantityDifference > 0), QuantityDifference, 0) QuantityDifferencePositive 
FROM    (           
            SELECT 
                     TempInventoryComparisonReal.Quantity - ISNULL([TempInventoryComparisonVirtual].[Quantity], 0)  QuantityDifference     
                FROM [TempInventoryComparisonReal]
                LEFT JOIN [TempInventoryComparisonVirtual] 
                ON ([TempInventoryComparisonReal].ItemsCoresId = [TempInventoryComparisonVirtual].ItemsCoresId 
                AND [TempInventoryComparisonReal].Dimensions1Id=[TempInventoryComparisonVirtual].Dimensions1Id
                AND [TempInventoryComparisonReal].Dimensions2Id=[TempInventoryComparisonVirtual].Dimensions2Id
                AND [TempInventoryComparisonReal].SerialNumber=[TempInventoryComparisonVirtual].SerialNumber)
        ) AS X

答案 3 :(得分:1)

您必须重复公式,查询分析器才能处理它。

SELECT (Field1 - Field2) QtyDiff,
       IIF((Field1 - Field2) > 0, (Field1 - Field2), 0) QtyDiffPositive