SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need help with a query..
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Kimi86
Yak Posting Veteran

54 Posts

Posted - 05/24/2012 :  12:52:31  Show Profile  Reply with Quote
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 example

Scripts:
-------
Create Table #T1
(
Id int,
Descp varchar(10)
)

Insert into #T1
Select 1,'One'
Union
Select 2,'Two'
Union
Select 3,'Three'
Union
Select 4,'Four'
Union
Select 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 #T1
Select * from #T2
Select * from #T3

Expected Output:
-----------------

id Descp Ranks
1 One 1001
2 Two 1002
3 Three 1003
4 Four 1004
5 Five 1005

robvolk
Most Valuable Yak

USA
15566 Posts

Posted - 05/24/2012 :  13:00:43  Show Profile  Visit robvolk's Homepage  Reply with Quote
CREATE VIEW myView AS
SELECT A.ID, A.Descp, B.Ranks FROM T1
INNER JOIN T2 ON T1.ID=T2.ID
UNION ALL
SELECT A.ID, A.Descp, B.Ranks FROM T1
INNER JOIN T3 ON T1.ID=T3.ID
Note that you can't include temp tables in a view definition, so I changed them to regular table references.
Go to Top of Page

HenryFulmer
Yak Posting Veteran

USA
98 Posts

Posted - 05/24/2012 :  13:10:24  Show Profile  Reply with Quote

select #t1.*, a.Ranks from #t1 
inner join 
(select id, Ranks from #t2 union all select id, Ranks from #t3) a
on #t1.id= a.id
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48026 Posts

Posted - 05/24/2012 :  15:56:48  Show Profile  Reply with Quote
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) a
on #t1.id= a.id



sorry didnt understand how this will work

As 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

HenryFulmer
Yak Posting Veteran

USA
98 Posts

Posted - 05/25/2012 :  15:10:21  Show Profile  Reply with Quote
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) a
on #t1.id= a.id



sorry didnt understand how this will work

As 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 Ranks
1 One 1001
2 Two 1002
3 Three 1003
4 Four 1004
5 Five 1005


Here's the script I used:

Create Table #T1
(
Id int,
Descp varchar(10)
)

Insert into #T1 
Select 1,'One'
Union
Select 2,'Two'
Union
Select 3,'Three'
Union
Select 4,'Four'
Union
Select 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) a
on #t1.id= a.id


Is my logic flawed?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48026 Posts

Posted - 05/25/2012 :  15:32:36  Show Profile  Reply with Quote
oh ok...you're doing union all inside
sorry didnt notice that
looks fine now

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000