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)
 how to get the result from inner query

Author  Topic 

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-10-10 : 01:05:40
Hello All,
I have a query which is made by the union of 3 different queries.
this query returns me 3 amounts
I want to Sum all these 3 amounts in same query.

my query is like

Select Amount1 from table 1
select Amount2 from table 2
select Amount3 from table3

I juts want to sum the resultant in a Column in Same Query.

Can Any one help me.

Thanks in advance.

I want to get the

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-10 : 01:16:34
[code]select sum(case when cat=1 then val else 0 end)as amt1,
sum(case when cat=2 then val else 0 end)as amt2,
sum(case when cat=3 then val else 0 end)as amt3,
sum(val) as total
from
(
Select Amount1 as val,1 as cat from table 1
union all
select Amount2 as val,2 as cat from table 2
union all
select Amount3 as val,3 as cat from table3
)t[/code]
Go to Top of Page
   

- Advertisement -