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)
 Tablees Structure

Author  Topic 

javad.nikoo
Starting Member

17 Posts

Posted - 2011-07-23 : 02:15:21
HI
I have 2 table withe the same fild for ex
T1:
Id
Count
t1:
Id
Count

some time the data are the same
NOW ,What I Want,Let me explain
i need another table with this strucure
this table(t3) is like this
ID >>>comes from t1 or T2 (Id Filed(Join))
Count >> T1.count-t2.count(if T1.id was in t2(t2.id) else t1.conut or t2.count )


(I did IT but It cost tooo much for me )
Please What is The Best way Too join This Tables
[]==[]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-23 : 04:17:29
[code]
insert into T3
select id, sum([count])
from
(
select id,[count]
from t1
union all
select id,[count]
from t2
)t
group by id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

javad.nikoo
Starting Member

17 Posts

Posted - 2011-07-24 : 00:12:31
thanks for your Reply
but as i explained id do not want the sum of this tables count filed(t1.count and t2.count) what i want is this i want this t1.count-t2.count if the code were same in both of this table and if it does not put t1.count or t1.count in t3.count and union is so expensive my tables have too much data
Best Regard's
JAVAD NIKOO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-24 : 00:55:45
[code]select coalesce(t1.id,t2.id),ABS(t1.count-t2.count)
from t1
full join t2
in t1.id = t2.id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

javad.nikoo
Starting Member

17 Posts

Posted - 2011-07-25 : 00:17:04
hi
thanks visakh16
i test it and it work correctly for me

best Regard's
javad.nikoo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-25 : 00:28:33
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -