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)
 SQL query

Author  Topic 

kanchan
Starting Member

1 Post

Posted - 2007-11-02 : 02:31:44
Hi,

I have to write a SQL Query. The codition is like suppose u have three coloums id,type,amount. And you have to do sum n minus operation in the same query based on the type value. for example

id type amount
1 A 100
2 A 100
3 A 400
4 B 200

Now i need to compute total amount.like if the type is 'A' then add that to total and if type is 'B' then minus the same from the total.
The output for the above should be = 400

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-02 : 02:35:08
[code]select sum(case when type = 'A' then amount when type = 'B' then -amount end)
from yourtable[/code]


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

Go to Top of Page
   

- Advertisement -