Column Alias

  • Hi All,

    How to use a column alias in an expression?

    Example:-

    ------------------------------------------------------------

    SELECT

    Tran.Amount+Tran.Discount+Tran.Taxes AS [Nett]

    --,[Nett] / 6.5 AS [Payable]

    FROM

    Tran

    WHERE Tran.Amount > 10000

    ------------------------------------------------------------

    How to use that computed column in another expression.

    It is required Because if we use SubQuery for a specific column then for each time when we want that column, we have to pass a SubQuery.

    How to Solve this?

  • Bhavin_Bhatt25 (8/22/2009)


    ------------------------------------------------------------

    SELECT

    Tran.Amount+Tran.Discount+Tran.Taxes AS [Nett]

    --,[Nett] / 6.5 AS [Payable]

    FROM

    Tran

    WHERE Tran.Amount > 10000

    ------------------------------------------------------------

    If i'm correct, you have to use all 'actual' column names for the 'payable' derived column as well. I dont think you can use NETT derived column as a part of query.



    Pradeep Singh

  • You cannot use an alias in a the select/where/group by you would need to repeat your calculation.

  • you could put it in a subquery if you really need to

    SELECT [Nett] / 6.5 AS [Payable]

    from (select Tran.Amount+Tran.Discount+Tran.Taxes AS [Nett]

    from Tran

    where Tran.Amount > 10000) v

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply