我想要实现的是从@ temp2获取ID列以对应于@ temp1中的intA和intB列 我希望看到的最终结果输出看起来像:
intA intB 'asset1' 'asset2' 'name1' 'name2'
1 1 108 108 Cash Cash
1 2 108 109 Cash Commodities
1 3 108 138 Cash Stock
.
.
.
2 5 109 111 Commodities Equity
以下是我正在使用的一些示例数据:
declare @temp1 table
(
intA int,
intB int
)
insert @temp1
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 1,5 union all
select 2,1 union all
select 2,2 union all
select 2,3 union all
select 2,4 union all
select 2,5
select * from @temp1
declare @temp2 table
(
oneup int,
id int,
name varchar(30)
)
insert @temp2
select 1,108,'Cash' union all
select 2,109,'Commodities' union all
select 3,138,'Stock' union all
select 4,110,'Bonds' union all
select 5,111,'Equity'
select * from @temp2
select t1.*,t2.* from @temp1 t1
inner join @temp2 t2
on t1.intA = t2.oneup
我无法让联接正常工作,给我一个像我期望的输出。 使用SQL2008
感谢您的帮助!
答案 0 :(得分:1)
您需要加入@temp2
两次,一次为intA
,一次为intB
。
select t1.intA, t1.intB,
t2a.id as asset1, t2b.id as asset2,
t2a.name as name1, t2b.name as name2
from @temp1 t1
inner join @temp2 t2a
on t1.intA = t2a.oneup
inner join @temp2 t2b
on t1.intB = t2b.oneup