2015-05-22 75 views

回答

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; 
+0

非常感謝你 –

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;