Refer to a calculated column in query to calclulate another column

  • I want to calculate a column for a query as a function of table columns, say:

    Select ColA*ColB*ColC as ColMultiple from tblCustomer....

    Now, I'd like to calculate another column as a function of the first calculated column, say:

    Select ColD/ColMultiple as ColAverage from tblCustomer....

    Must I repeat the calculation of 'ColMultiple' each time I want to use that result, or is there a way to refer to the newly-calculated colum, say:

    Select ColA*ColB*ColC as ColMultiple, ColD/ColMultiple as ColAverage from tblCustomer....

    I know that doesn't work... is there a way to do it?

    Jim

  • You can use a CROSS APPLY.

    SELECT ColMultiple, ColD/ColMultiple as ColAverage

    FROM tblCustomer

    CROSS APPLY (

    SELECT ColA*ColB*ColC as ColMultiple

    ) AS cm

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 2 posts - 1 through 1 (of 1 total)

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