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
 SQL Server Development (2000)
 summing one field to return two columns?

Author  Topic 

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-12-27 : 16:30:15
I have a column called Amount. I'd like to write a query statement that returns two columns called int. amt and ext. amt, where both are based on separate conditions. This is what the query looks like now:

select sum(amount) as int amt from Accountlist where type = 'int'
select sum(amount) from ext amt Accountlist where type = 'ext'

Something like this:

int amt| ext amt
-------|--------
5000 |6000

instead of this:
amt
-------
5000
6000

Is it possible?

nr
SQLTeam MVY

12543 Posts

Posted - 2007-12-27 : 19:45:48
select intamt = (select sum(amount) from Accountlist where type = 'int') ,
extamt = (select sum(amount) from Accountlist where type = 'ext')

or
select intamt = sum(case when type = 'int' then amount else 0 end) ,
extamt = sum(case when type = 'ext' then amount else 0 end)
from Accountlist



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2007-12-28 : 09:31:45
THANK YOU SO MUCH!

That is exactly what I needed. I would've never thought of using case inside sum or "=" in a query statement. I thought these things only can be used in procedure-type statements, but this has opened up new possibilities so thank you again!
Go to Top of Page
   

- Advertisement -