Home > Blockchain >  SAS SQL inner join two columns able to match one column
SAS SQL inner join two columns able to match one column

Time:04-06

How can I do something like select * from T1 inner join T2 on (T1.ID=T2.ID OR T1.ID2=T2.ID) When I execute this code, it seems to fall in a infinity loop so I guess I'm wrong.

In other words, how can I match one of two columns from T1 to one column from T2

T1

ID ID2
1  10
2  20

T2

ID  value
1   dummy10
20  dummy20

Result

ID ID2 value
1  10  dummy10
2  20  dummy20

CodePudding user response:

Try to do like this:

select *
from T1, T2
where T1.ID = T2.ID or T1.ID2 = T2.ID

CodePudding user response:

you can use 2 select statements with union, like this:

select 
    t1.ID, 
    t1.ID2, 
    t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID = t2.ID

UNION 

select 
    t1.ID, 
    t1.ID2, 
    t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID2 = t2.ID
/* this will exclude values selected by other statement */
where t1.ID2 not in (select ID2 from Table1 inner join Table2 on Table1.ID = Table2.ID)
  • Related