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
 Urgent - how to get difference sum

Author  Topic 

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-10-13 : 00:21:16
Hi i have a table as below

year month income source
___________________________
2008 06 100 source1
2008 06 80 source2
2008 06 200 source1
2008 06 100 source2

my output is

year month income
___________________
2008 06 120 (sum(rev) source1 - sum(rev) source2)

may i know what is the SQL query statement can do that????

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-13 : 00:30:04
[code]SELECT year,month,
SUM(CASE WHEN source='source1' THEN income ELSE 0 END)-SUM(CASE WHEN source='source2' THEN income ELSE 0 END) AS income
FROM Table
GROUP BY year,month[/code]
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-10-13 : 02:02:48
quote:
Originally posted by visakh16

SELECT year,month,
SUM(CASE WHEN source='source1' THEN income ELSE 0 END)-SUM(CASE WHEN source='source2' THEN income ELSE 0 END) AS income
FROM Table
GROUP BY year,month





Thank you a lot visakh16.

Wish you all the best in your life.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-13 : 02:44:19
You're welcome
thanks for the wish
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-10-13 : 02:48:15
quote:
Originally posted by visakh16

You're welcome
thanks for the wish



Welcome
Go to Top of Page
   

- Advertisement -