Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting something as two different fields

Author  Topic 

Mmats
Starting Member

47 Posts

Posted - 2005-04-07 : 14:14:38
I want to do something like the following:
Select sum(q5)/ count(q5) as [q5 overall mean],
sum(q5)/ count(q5) as [q5_temp]

Is there a way to do the calculation only once, since its the same?

rfrancisco
Yak Posting Veteran

95 Posts

Posted - 2005-04-07 : 14:31:04
I don't think there's a way to do this, unless I'm mistaken.
Go to Top of Page

PW
Yak Posting Veteran

95 Posts

Posted - 2005-04-07 : 14:55:56
Have you actually run a showplan on this to verify that Sql Server performs the calculation twice ?
I would expect to see query plan steps of "Stream Aggregate/Aggregate" and "Compute Scalar", where the 1st performs the aggregate calculation, and the 2nd simply assigns the already computed value to the 2nd column.
Go to Top of Page

Kaleem021
Starting Member

26 Posts

Posted - 2005-04-08 : 01:04:39
Try this if [q5 overall mean] remains same for every record of your select query.
Declare @q5 As Float
Set @q5 = (Select sum(q5)/ cast(count(q5) as Float) from <Your Table Name>)
SELECT @q5 as [q5 overall mean], @q5 as q5_temp from <Your Table Name>



*****************************************************************************
Myth Breaker
Kaleem021@hotmail.com

Doing Nothing Is Very Hard To Do, You Never Know When You Are Finished.
Go to Top of Page
   

- Advertisement -