sas在选择选项中选择,使其更容易

时间:2014-06-12 19:49:40

标签: sas

我有SAS代码作为SELECT IN SELECT。但是选择select会使运行缓慢。如果没有在select中选择,我怎样才能使这段代码变得更容易:

    select distinct
SPRIDEN.SPRIDEN_ID ,rcrapp4_email_address
   from sTG.SPRIDEN, stg.sprtele,stg.rcrapp1, stg.rcrapp4 o
 where and rcrapp1_curr_rec_ind = 'Y'
 and rcrapp1_pidm = rcrapp4_pidm
 and rcrapp1_infc_code = rcrapp4_infc_code
 and rcrapp1_seq_no = rcrapp4_seq_no
 and rcrapp1_aidy_code = rcrapp4_aidy_code
 and rcrapp4_aidy_code = '1314'
 and rcrapp4_activity_date =
 (select max(rcrapp4_activity_date)
 from stg.rcrapp4 i
 where i.rcrapp4_pidm = o.rcrapp4_pidm
 and i.rcrapp4_aidy_code = o.rcrapp4_aidy_code
and i.rcrapp4_infc_code = o.rcrapp4_infc_code
 and i.rcrapp4_seq_no = o.rcrapp4_seq_no)

我需要将此代码设为easir,而不选择select。谢谢

2 个答案:

答案 0 :(得分:0)

让我让代码缩短,你能不能在播放代码时提出想法

   select distinct
  SPRIDEN.SPRIDEN_ID ,rcrapp4_email_address
   from sTG.SPRIDEN, stg.rcrapp4 o
  where 
 rcrapp1_pidm = rcrapp4_pidm
and rcrapp4_activity_date = (select max(rcrapp4_activity_date)
        from stg.rcrapp4 i
      where i.rcrapp4_pidm = o.rcrapp4_pidm
       and i.rcrapp4_seq_no = o.rcrapp4_seq_no)

答案 1 :(得分:0)

而不是你的相关子查询,我会尝试这样的连接:

    select distinct SPRIDEN.SPRIDEN_ID, rcrapp4_email_address
    from stg.SPRIDEN s
    inner join stg.rcrapp4 o on s.rcrapp1_pidm = o.rcrapp4_pidm
    inner join ( select rcrapp4_pidm, rcrapp4_seq_no,
                 max(rcrapp4_activity_date) as max_rcrapp4_activity_date
            from stg.rcrapp4
            group by rcrapp4_pidm, rcrapp4_seq_no ) i
    on o.rcrapp4_pidm = i.rcrapp4_pidm
       and  o.rcrapp4_seq_no= i.rcrapp4_seq_no
    and o.rcrapp4_activity_date = i.max_rcrapp4_activity_date

即,获取子查询中的最大日期 或者:

在别名“i”中从子查询构建临时表,在连接列rcrapp4_pidm,rcrapp4_seq_no上对其进行索引,并对临时表执行相同的连接。确保确实需要distinct

options msglevel=I; /* to see index usage */
proc sql;
create table WORK.LATEST_ACTIVITY as
select rcrapp4_pidm, rcrapp4_seq_no,
                 max(rcrapp4_activity_date) as max_rcrapp4_activity_date
            from stg.rcrapp4
            group by rcrapp4_pidm, rcrapp4_seq_no
;
create index ix on WORK.LATEST_ACTIVITY (rcrapp4_pidm, rcrapp4_seq_no)
;
create table WORK.RESULT as
select distinct SPRIDEN.SPRIDEN_ID, rcrapp4_email_address
    from stg.SPRIDEN s
    inner join stg.rcrapp4 o on s.rcrapp1_pidm = o.rcrapp4_pidm
    inner join work.latest_activity i
    on o.rcrapp4_pidm = i.rcrapp4_pidm
       and  o.rcrapp4_seq_no= i.rcrapp4_seq_no
    and o.rcrapp4_activity_date = i.max_rcrapp4_activity_date
;
quit;