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
 Select SUM Problem

Author  Topic 

joelseverich
Starting Member

34 Posts

Posted - 2010-12-29 : 15:33:43
Hi,
I tried to execute this query

select sum(TableA.Cost) AS CostoA, SUM(TableB.Cost) AS CostB FROM TableA,TableB

so this is in tableA
1000.00
and this is in tableB
10.00
20.00

and the result is

CostA CostB
2000.00 60.00

would you please help me on this??

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-29 : 15:40:21
You've done a cross join, hence the problem.

Can they be in different rows?

select 'CostA', sum(TableA.Cost) AS Cost
FROM TableA
UNION ALL
select 'CostB', SUM(TableB.Cost)
FROM TableB

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-12-30 : 01:44:23
or you wanted this ?

select (select SUM(TableA.Cost) from TableA) AS CostoA,
(select SUM(TableB.Cost) from TableB) AS CostB



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

Go to Top of Page
   

- Advertisement -