If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > query referring to earlier part of same query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-11, 11:50
WDS WDS is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Question 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!
Reply With Quote
  #2 (permalink)  
Old 08-12-11, 11:56
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
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
...
)
Reply With Quote
  #3 (permalink)  
Old 08-12-11, 12:00
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On