Aliasing and Calculations

  • I am facing a problem while calculating a field from an aliased field. The err that it gives me is: Invalid Column name

    This is my code:

    select

    case EmployeesVestingSchedule.BasedOn

    when 'P' then Portion

    when 'T' then 0

    end 'PerformanceBased_PerCent' ,

    PerformanceBased_PerCent * Employee_Options.NoOfOptions/100as PerformanceBased_Numbers, ---- this gives an error ----

    Employee_Options.NoOfOptions

    from

    EmployeesVestingSchedule , employee_options

    Paras Shah

    Evision Technologies

    Mumbai, India


    Paras Shah
    Evision Technologies
    Mumbai, India

  • This is because the 'PerformanceBased_PerCent' isn't yet available. The aliased field can't be used by your expression. You'll find that the aliased column is also unavailable in the WHERE, GROUP BY, or HAVING clauses of a SELECT statement as well. They are available in the ORDER BY clause. You may have to go to a subquery.

    K. Brian Kelley

    bk@warpdrivedesign.org

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • Use a subquery or a view for the reasons outlined by Brian.

    Steve Jones

    steve@dkranch.net

  • You might also considering storing this value as a denormalized column on your table. You can add a CHECK constraint to enforce your business rule. Since you're trying to influence the output of SQL with this value, it's a good candidate for an actual field instead of one generated through the query.

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

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