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)
 Derived Query

Author  Topic 

renu
Starting Member

47 Posts

Posted - 2007-12-13 : 01:22:41
There are two tables.

TABLE A: c1 , amount1

table B:c2 ,amount2

now i ahve to fine the difference between amount1 and amount2 for all

corresponding c1 and c2 column,

i used full outer join,i am getting the duplicated values,

so any other method?(derived query)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-13 : 01:24:37
What is the primary key of Table A & B ?

Can you show us your query ?




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

Go to Top of Page

renu
Starting Member

47 Posts

Posted - 2007-12-13 : 01:35:24
select sum(isnull(amount1,0)-isnull(amount2,0)) as balance
from ( select distinct r1.c1,r1.amount1,r2.c2,r2.amount2
from a as r1
full outer join (select r2.c2,r2.amount2
from b as r2)c on a.c1=c.c2)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-13 : 01:37:36
can you also show us what do you mean by duplicated values ? Please post some sample data of such scenario


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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-13 : 01:53:02
Also try

select c1,sum(amount1) - sum(amount2) as diff from
(
Select c1, amount1,0 as amount2 from table_a
union all
Select c2, 0,amount2 from table_b
) as t


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -