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 2005 Forums
 Transact-SQL (2005)
 Using Column Created in Select Statement with 'AS'

Author  Topic 

batcater98
Starting Member

22 Posts

Posted - 2008-07-30 : 15:55:13
I have a select statement that creates new columns derived from other table items. I then want to use those created columns in the same select statement to create more.

Example:

select sum(e.val1 * e.val2/100) as Key1, Key1 / e.val2 as Key3

I can do this in access, but can not figure out how to do the same thing in MSSQL.

I want to use the value created and stored in Key1 to make Key3.

Is this possible in MSSQL?

Thanks,
Ad.

Regards,
The Dark Knight
-Give What is Right, Not What is Left-

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-30 : 15:59:46
You can do it with a derived table:


select key1, key1/e.val2 as key3
from
(select sum(e.val1 * e.val2/100) as Key1) t
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-31 : 04:11:13
or use the expression in place of column

select sum(e.val1 * e.val2/100) as Key1, sum(e.val1 * e.val2/100) / e.val2 as Key3

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -