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
 Subscription two columns from two tables

Author  Topic 

dotnet2010
Starting Member

7 Posts

Posted - 2011-08-01 : 05:24:01
Hi,
I have two values from the same table I want to do a join between the two selects results and show the Id Column and V1-V2
The first Select Query Reult from Table T1 Where X groupBy Id
ID V1
5 2
7 1
19 9
31 1

The Second Select Query Result from Table T2 Where Y GroupBy Id
ID V2
5 1
7 0
19 5

I want to show this new table {Id ,(V1-V2) as NewV}
Id NewV (V1-V2)
5 1
7 1
19 4

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-01 : 05:28:29
[code]
select t1.ID, newV = t1.V1 - t2.V2
from
(
select ID, V1 = sum(V1)
from sometable
group by ID
) t1
inner join
(
select ID, V2 = sum(V2)
from sometable2
group by ID
) t2 on t1.ID = t2.ID
[/code]


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

Go to Top of Page
   

- Advertisement -