Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expressions in "select" and "order by"

Status
Not open for further replies.

alexerm

Programmer
Feb 27, 2003
17
0
0
CA
Hi,

Here is sql type of query I'm trying to execute:

SELECT a=3,b=4,c=a+b

However, it does not let me do it, because I'm trying to use variebles "a" and "b" in calculating "c".

Any suggestions, why it does not let me do it?

P.S. In my real query "a" and "b" are calculated using complicated subqueries, so I don't want to repeat those subqueries in calculating "c".

Thanks,

Alex
 
Hi,

Try this query

Select a,b,a+b c From
(SELECT a=3,b=4) tbl

Sunil
 
How about this one?

declare @a int
declare @b int
declare @c int

set @a=3
set @b=4

select @c = @a+@b

print @c
 
Or you could just do:

Code:
SELECT a = 3, b = 4
SELECT c = a + b

;-) --James
 
Do your complex calculations in a sub-query and then select from the sub-query to perform the final calculation.

SELECT qry.a, qry.b, c = qry.a + qry.b
From
(Select a=<complex formula>, b=<another complex formula>
From yourtable Where <some criteria>) As qry If you want to get the best answer for your question read faq183-874 and thread183-468158.
Terry L. Broadbent - DBA
SQL Server Page:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top