Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    4

    Question Unanswered: query referring to earlier part of same query

    Suppose I have a query that starts something like this:

    SELECT
    (4.179 * (Avg_Flow_Rate * 0.453597) * (30 * 24) * (Avg_Out_Temp - Avg_Return_Temp)) / 3600 AS Usage_kWh,
    (4.179 * (Avg_Flow_Rate * 0.453597) * (30 * 24) * (Avg_Out_Temp - Avg_Return_Temp)) / 3600 * 3412.14163 AS Usage_BTU,
    ...

    Is there any way in the second formula to refer back to the field from the first one? Something like:

    SELECT
    (4.179 * (Avg_Flow_Rate * 0.453597) * (30 * 24) * (Avg_Out_Temp - Avg_Return_Temp)) / 3600 AS Usage_kWh,
    Usage_kWh * 3412.14163 AS Usage_BTU,
    ...

    I know I could set up a temporary table but how about within one query?

    Thanks!

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Code:
    SELECT
     Usage_kWh,
     Usage_kWh * 3412.14163 AS Usage_BTU
    FROM (
     SELECT
    (4.179 * (Avg_Flow_Rate * 0.453597) * (30 * 24) * (Avg_Out_Temp - Avg_Return_Temp)) / 3600 AS Usage_kWh
    ...
    )

  3. #3
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    What do you want to mean by "temporary table"?
    If it is table expression, I think no other way.
    I want to note there are two types of table expressions, common table expression and nested table expression.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •