1

我想知道是否可以加入我们正在创建的同一个表,而不必在加入后复制整个表代码。

例如:

create table tableC as
select * 
from (
    select *, min(a) as minA 
     from tableB
     group by id) as tb0 
) 
join (select *, min(a) as minA 
    from tableB
    where min(a) = 1) as tb1
on tb1.id = tb0.id;

在此示例中,加入不是必需的,但在某些情况下仍然是必需的。我的问题是我们可以使用第一个块代码中的表来执行连接,而不必在连接后复制整个代码吗?

更准确地说,我们可以做类似的事情

create table tableC as
select * 
from (
    select *, min(a) as minA 
     from tableB
     group by id) as tb0 
) 
join (select *
    from **tb0**
    where **minA** = 1) as tb1
on tb1.id = tb0.id;

谢谢

4

2 回答 2

2

好吧,干净简单的方法是创建一个视图:

proc sql noprint;
    create view myView as
    select *, min(a) as minA 
    from tableB
    group by id;
quit;

proc sql noprint;
    select *
    from myView as tb0
    join
    (
        select *
        from myView
        where minA=1
    ) as tb1
    on tb1.id=tb2.id;
quit;

它可以工作(如果我没有打错字),它可以防止代码重复并且看起来更干净。(后者当然是个人意见)

我想知道您想象这个特定连接在什么情况下有用。

于 2013-08-12T09:45:06.413 回答
1

在 SAS 中,您不能直接执行该功能。在将使用with块执行的 TSQL 中,但 SAS(当前)不支持该功能。这看起来大致如下:

with tb0 as (
    select *, min(a) as minA 
     from tableB
     group by id)
select * from tb0 
 inner join
 (select 1 from tb0 where min(something)) tb1
  on tb0.id=tb1.id;
于 2013-08-12T14:00:32.590 回答