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)
 operations with subqueries results - in same state

Author  Topic 

johnstern
Yak Posting Veteran

67 Posts

Posted - 2007-11-23 : 19:23:12
lets say I have somehting like this:

select
ContactName,

(select sum(Freight) from orders where customerID = c.customerID)as freight,
(freight + 1 ) as freightplusone

from Customers c

this gives me an error it does not let me take the freight ( result of the sum of a subquery and add a 1 to it.

is there any way that i can do this ? I would have thought that this results of the subqueries would be treated like columns so they should add 1 without any problem

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-23 : 19:37:04
you would have to do the subquery again...
[code]
select
ContactName,

(select sum(Freight) from orders where customerID = c.customerID)as freight,
((select sum(Freight) from orders where customerID = c.customerID) + 1 ) as freightplusone

from Customers c
[code]

But this makes more sense

Select Sum(Frieght) as Frieght,sum(Frieght) + 1 as Freight+1, c.CustomerID
FROM Customers c inner join Orders o on c.CustomerID = o.CustomerID
WHERE c.CustomerID = ????




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page
   

- Advertisement -