Oracle Prior date Query

时间:2014-12-19 17:20:26

标签: sql oracle11g informatica

我在从日期维度获取明确的先前报告日期时遇到问题。我需要为给定的日历日期填充12个以前的报告日期(不同)。此报告日期填充在caertian业务规则中。对于instnace

CAL_DATE    RPT_DATE
22-Nov-14   24-Nov-14
23-Nov-14   24-Nov-14
24-Nov-14   24-Nov-14
25-Nov-14   25-Nov-14
26-Nov-14   26-Nov-14
27-Nov-14   1-Dec-14
28-Nov-14   1-Dec-14
29-Nov-14   1-Dec-14
30-Nov-14   1-Dec-14
1-Dec-14    1-Dec-14
2-Dec-14    2-Dec-14
3-Dec-14    3-Dec-14
4-Dec-14    4-Dec-14
5-Dec-14    5-Dec-14
6-Dec-14    8-Dec-14
7-Dec-14    8-Dec-14

我正在寻找的是

DAT_DATE_   RPT_DT  PRIOR1  PRIOR12 PRIOR3  PRIOR4  PRIOR5  PRIOR6
4-Dec-14    3-Dec-14    2-Dec-14    1-Dec-14    26-Nov-14   25-Nov-14   24-Nov-14   21-Nov-14
3-Dec-14    2-Dec-14    1-Dec-14    26-Nov-14   25-Nov-14   24-Nov-14   21-Nov-14   20-Nov-14

我用于此的查询是

select DAT_DATE_DT,dat_sls_wrk_rpt_dt,Prior1,Prior2,Prior3,Prior4,Prior5,Prior6,Prior7,Prior8,Prior9,Prior10  from (
  select DAT_DATE_DT,
  dat_sls_wrk_rpt_dt,
  lag(dat_sls_wrk_rpt_dt,1)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior1,
  lag(dat_sls_wrk_rpt_dt,2)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior2,
  lag(dat_sls_wrk_rpt_dt,3)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior3,
  lag(dat_sls_wrk_rpt_dt,4)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior4,
  lag(dat_sls_wrk_rpt_dt,5)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior5,
  lag(dat_sls_wrk_rpt_dt,6)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior6,
  lag(dat_sls_wrk_rpt_dt,7)over (partition by DAT_DATE_DT order by dat_sls_wrk_rpt_dt ) AS Prior7,
  RANK 
FROM
  ( SELECT DISTINCT B.DAT_DATE_DT,
  A.dat_sls_wrk_rpt_dt,
  dense_rank() over (partition by B.DAT_DATE_DT order by A.dat_sls_wrk_rpt_dt DESC) RANK
  FROM EWT_DATE_DIM1 A,
    EWT_DATE_DIM1 B
  WHERE A.dat_sls_wrk_rpt_dt < B.dat_sls_wrk_rpt_dt
  AND B.DAT_DATE_DT in (trunc(sysdate-15),trunc(sysdate-16) )
  )
WHERE RANK <12)
where RANK=1
ORDER BY 2 DESC

任何人都可以想到比这更简单的事情。由于历史载荷可能有很多日期,我想对此进行微调。此外,我需要在informatica中完成。

1 个答案:

答案 0 :(得分:0)

我想我会通过存储过程为每个感兴趣的日期执行此操作。您的查询可能有效,但我认为它不会很有效。我不熟悉informatica的确切语法,但是这样:

declare dCursor CURSOR for  
select dat_date_dt, dat_sql_wrk_rpt_dt 
from EWT_DATE_DIM1
where EWT_DATE_DIM1.DAT < @dt
order by dat_date_dt desc

然后只收集不同的值。这将更有效,特别是对于dat_date_dt

的索引和索引