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.
Author |
Topic |
Kimi86
Yak Posting Veteran
79 Posts |
Posted - 2012-05-24 : 12:52:31
|
Hi All,I have two table with same kind of data but different keys. One is History and one is active.I need to merge the rerults in a view joing them with a thrird Taable.Below are scripts for a simplified exampleScripts:------- Create Table #T1 ( Id int, Descp varchar(10) ) Insert into #T1 Select 1,'One'UnionSelect 2,'Two'UnionSelect 3,'Three'UnionSelect 4,'Four'UnionSelect 5,'Five'Select Id,id + 1000 Ranks into #T2 from #T1 where ID in (1,2,3)Select Id,id + 1000 Ranks into #T3 from #T1 where ID in (4,5)Select * from #T1Select * from #T2Select * from #T3Expected Output:-----------------id Descp Ranks1 One 10012 Two 10023 Three 10034 Four 10045 Five 1005 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-24 : 13:00:43
|
[code]CREATE VIEW myView ASSELECT A.ID, A.Descp, B.Ranks FROM T1INNER JOIN T2 ON T1.ID=T2.IDUNION ALLSELECT A.ID, A.Descp, B.Ranks FROM T1INNER JOIN T3 ON T1.ID=T3.ID[/code]Note that you can't include temp tables in a view definition, so I changed them to regular table references. |
 |
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2012-05-24 : 13:10:24
|
[code]select #t1.*, a.Ranks from #t1 inner join (select id, Ranks from #t2 union all select id, Ranks from #t3) aon #t1.id= a.id[/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-24 : 15:56:48
|
quote: Originally posted by HenryFulmer
select #t1.*, a.Ranks from #t1 inner join (select id, Ranks from #t2 union all select id, Ranks from #t3) aon #t1.id= a.id
sorry didnt understand how this will workAs i see OP is populating #T1 and #t2 with different sets of id so i dont think join on id will return anything------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2012-05-25 : 15:10:21
|
quote: Originally posted by visakh16
quote: Originally posted by HenryFulmer
select #t1.*, a.Ranks from #t1 inner join (select id, Ranks from #t2 union all select id, Ranks from #t3) aon #t1.id= a.id
sorry didnt understand how this will workAs i see OP is populating #T1 and #t2 with different sets of id so i dont think join on id will return anything----------------------------------------------------------------------
Using the example from the original post this is the result set that is being returned:id Descp Ranks1 One 10012 Two 10023 Three 10034 Four 10045 Five 1005Here's the script I used:Create Table #T1(Id int,Descp varchar(10))Insert into #T1 Select 1,'One'UnionSelect 2,'Two'UnionSelect 3,'Three'UnionSelect 4,'Four'UnionSelect 5,'Five'Select Id,id + 1000 Ranks into #T2 from #T1 where ID in (1,2,3)Select Id ,id + 1000 Ranks into #T3 from #T1 where ID in (4,5)select #t1.*, a.Ranks from #t1 inner join (select id, Ranks from #t2 union all select id, Ranks from #t3) aon #t1.id= a.id Is my logic flawed? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-25 : 15:32:36
|
oh ok...you're doing union all insidesorry didnt notice thatlooks fine now------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|