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)
 Subquery

Author  Topic 

monkeyvu
Starting Member

9 Posts

Posted - 2007-10-18 : 00:55:55
Hi all,

I'm having an issues with subquery. My query is

Select (subquery1) as t1, (subquery2) as t2, (subquery2 - subquery1) as B from ...

But I dont want to use (subquery2 - subquery1) because those queries are very big . Is there any solution for this? Thanks for your help

Kristen
Test

22859 Posts

Posted - 2007-10-18 : 01:04:31
[code]
Select t1, t2, (t2 - t1) as B
FROM
(
Select (subquery1) as t1, (subquery2) as t2 from ...
) AS X
[/code]
Kristen
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-18 : 01:53:41
DECLARE @sq1 INT, @sq2 INT

SELECT @sq1 = {subquery 1}
SELECT @sq1 = {subquery 2}

SELECT @sq1 AS t1, @sq2 as t2, @sq2 - @sq1 as t3

or if NULL can be the case and NULL should be treated as zero.

SELECT coalesce(@sq1, 0) AS t1, coalesce(@sq2, 0) as t2, coalesce(@sq2, 0) - coalesce(@sq1, 0) as t3


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

monkeyvu
Starting Member

9 Posts

Posted - 2007-10-19 : 00:00:39
Thank you guys :-)
Go to Top of Page
   

- Advertisement -