Proc Sql - 来自 3 个表的相同列名并进行操作

Proc Sql - Same column name from 3 tables and manipulate

这是示例数据。

表 1:

iD Flag  Reason
1    1     ABCD
2    0     
3    0     
4    1     ERS
5    0     

表 2:

iD Flag  Reason
1    1     ERS
2    1     FGH
3    1     DDD
4    1     
5    0     

表 3:

iD Flag  Reason
1    1     
2    1    DDD
3    1     
4    1     
5    1    ERS 

我正在尝试编写一个程序 sql 来帮助我区分不同的原因案例场景。

proc sql;
select case when (table1.flag = 1 and reason = 'ABCD') then 1
        when (table2.flag = 1 and reason = 'ABCD') then 1
        when (table3.flag = 1 and reason = 'ABCD') then 1
        when (table1.flag = 1 and reason = 'FGH') then 2
        when (table2.flag = 1 and reason = 'FGH') then 2
          :
          :
        when (table3.flag = 1 and reason = 'ERS') then 4
        Else 0
        End as output
   from table1
   inner join table2 on table1.id = table2.id
   inner join table3 on table1.id = table3.id
   ;

这是我想要得到的输出,

 Id Reason Output 
 1  ABCD     1
 2  FGH      2
 3  DDD      3 
 4  ERS      4
 5  ERS      4 

尽管如此,同一 ID 的原因可能会在不同的 table 中发生变化,但我需要根据 Flag 变量在任何 table 中第一个出现的原因并分配输出。

还有其他方法可以做到吗?

提前致谢。

以下查询会生成您要查找的输出:

select t1.id,
       (case when t1.flag = 1 then t1.reason
             when t2.flag = 1 then t2.reason
             when t3.flag = 1 then t3.reason
        end) as reason,
       (case when calculated reason = 'ABCD' then 1
             when calculated reason = 'FGH' then 2
             when calculated reason = 'DDD' then 3
             when calculated reason = 'FGH' then 4
             else 0
        end) as output
from table1 t1 inner join
     table2 t2 
     on t1.id = t2.id inner join
     table3 t3
     on t1.id = t3.id;

注意 calculated 关键字的使用。这是 proc sql 中的一个很好的特性,它使得不必使用子查询或重复定义表达式。