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)
 Should I use variables for this?!

Author  Topic 

Antonio
Posting Yak Master

168 Posts

Posted - 2005-10-25 : 07:19:43
Good afternoon,

I have the following two queries :


SELECT a.[date] as iDate,
SUM(a.struck_stake) as struck_stakes,
sum(a.struck_stake)-sum(settled_returns) as GP
FROM
AGG1.dbo.AGG_DAY_009 as a
inner join WHouse1.dbo.Date_Dim as b
on b.date_key = a.DATE
inner join WHouse1.dbo.COST_CENTRE_DIM as c
on c.COST_CENTRE_NUMBER = a.COST_CENTRE_NUMBER
WHERE c.CURRENT_FLAG = 'C'
AND a.Date = 20051019
AND c.COST_CENTRE_NUMBER = 2309
group by a.[date]

(select b.df1date as iDate, b.take as struck_stakes, b.adjustedGP as GP
from whouse1.dbo.date_dim as a
inner join whouse1.dbo.daily_figures_fact as b
on a.date_key = b.df1date
inner join whouse1.dbo.cost_centre_dim as c
on c.cost_centre_code = b.shop
where (c.current_flag = 'c' or c.current_flag = 'x')
and b.df_username <> 'system'
and c.cost_centre_code = 2309
and a.date_key = 20051019)


I want to subtract the struck_stakes in the first query from the struck_stakes in the second one. I thought about joining the two tables (they both return one record each) on the date then subtract the values but I have a feeling that there is a better way to do it by assiginig the values to variables within the select statment.

How can this be done?

Many thanks in advance.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-05 : 07:59:35
How many rows do each query resturns?
If they return only one row, you can assign it a varaible and subtract it. Otherwise you need to join them and do subtraction

Madhivanan

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

- Advertisement -