Oracle:根据基于期间的先前记录获取价值

时间:2015-05-22 12:18:59

标签: sql oracle window-functions

由于安全原因我删除了表名.Sorry

2 个答案:

答案 0 :(得分:0)

我认为您需要添加partition by

SELECT pgm_id, pml.period_id, medi_cov_lives,
       LAG(medi_cov_lives, 1, 0)
          OVER (PARTITION BY pgm_id ORDER BY pml.pgm_id, pml.period_id
               ) AS pre_medi_cov_lives
FROM <table> pml;

答案 1 :(得分:0)

您需要指定lag分别处理每个pgm_id,并使用partition by子句:

CREATE OR REPLACE FORCE VIEW HCRS.PGM_MEDI_LIVES_V
(
   PGM_ID,
   PERIOD_ID,
   MEDI_COV_LIVES,
   PREV_MEDI_COV_LIVES
)
AS
   SELECT 
         pgm_id,
         pml.period_id,
         medi_cov_lives,
         LAG (medi_cov_lives, 1, 0)
             OVER (PARTITION BY pml.pgm_id ORDER BY pml.period_id)
             AS pre_medi_cov_lives
     FROM PGM_MEDI_LIVES pml;