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
 General SQL Server Forums
 New to SQL Server Programming
 Calculating a difference between two columns

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2012-08-23 : 04:27:25
hi guys

this is my current query

SELECT [fdmsaccountno],
Case when year(hst_date_processed) = '2011' then SUM ([msc]) else 0 end as [2011] ,
Case when year(hst_date_processed) = '2012' then SUM ([msc]) else 0 end as [2012]
,[MSC]
,[Refund Transaction Charge]
,[PCI]
,[Joining Fee]
,[Other]
FROM [Rm_Book_New].[dbo].[tmpbd1]
Where fdmsaccountno = '878020388889'
group by fdmsaccountno,[MSC]
,[Refund Transaction Charge]
,[PCI]
,[Joining Fee]
,[Other],hst_date_processed )

select fdmsaccountno,SUM([2011]) as [2011 MSC],SUM([2012]) as [2012 msc] from cte
group by fdmsaccountno


which returns the following

fdmsaccountno 2011 MSC 2012 msc
878020388889 13418.77 6596.75

how can i create a column which works out the difference between 2011 msc and 2012 ?

so in this example the difference is -6822.02

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2012-08-23 : 04:45:51
Just add:

SUM([2011]) - SUM([2012]) as difference

to your query from the cte
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2012-08-23 : 05:10:43
Hi RickD

Where in my query would i put that ?
Sorry i am new to sql, and only know how to do certain things.

Sorry to be a burden
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-23 : 05:37:41
[code]select fdmsaccountno,
SUM([2011]) as [2011 MSC],
SUM([2012]) as [2012 msc],
SUM([2011]) - SUM([2012]) as [Diff]
from cte
group by fdmsaccountno[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2012-08-23 : 05:40:04
cheers guys :)
Go to Top of Page
   

- Advertisement -