2011-02-15 7 views
2

도움을 얻으려는 시도는 @ temp2의 intA 및 intB 열 모두에 해당하는 @ temp2의 ID 열을 얻는 것입니다. 최종 결과 결과가 뭔가 보일 것이라고 기대합니다. 같은 :MS-SQL 쿼리에 대한 도움이 필요합니다.

여기
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 샘플 데이터 포함. –

답변

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 
+0

완벽! 정확히 내가 무엇을 찾고 있었는지. 두 번째 조인 사용에 대해 생각하지 않았습니다. 감사! –

관련 문제