2
我有下表。用戶last_value填寫日期
MY_DAY | STMT_DATE ------------+------------- 12/31/2013 | 12/31/2013 1/1/2014 | 1/2/2014 | 1/2/2014 1/3/2014 | 1/3/2014 1/4/2014 | 1/5/2014 | 1/6/2014 | 1/6/2014 1/7/2014 | 1/7/2014 1/8/2014 | 1/8/2014 1/9/2014 | 1/9/2014 1/10/2014 | 1/10/2014 1/11/2014 | 1/12/2014 | 1/13/2014 | 1/13/2014 1/14/2014 | 1/14/2014
我想爲每一行獲取STMT_DATE的前一個和後一個非空值。我使用下面的查詢:
SELECT my_day,
stmt_date,
first_VALUE (stmt_date IGNORE NULLS) OVER (ORDER BY stmt_date asc ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) previous,
first_VALUE (stmt_date IGNORE NULLS) OVER (ORDER BY stmt_date desc ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) next
FROM DATE_TEST
ORDER BY my_day
但我發現了以下結果:
MY_DAY | STMT_DATE | PREVIOUS | NEXT -----------+------------+------------+-------------- 12/31/2013 | 12/31/2013 | | 1/1/2014 | | 12/31/2013 | 1/14/2014 1/2/2014 | 1/2/2014 | 12/31/2013 | 12/31/2013 1/3/2014 | 1/3/2014 | 12/31/2013 | 1/2/2014 1/4/2014 | | 12/31/2013 | 1/14/2014 1/5/2014 | | 12/31/2013 | 1/14/2014 1/6/2014 | 1/6/2014 | 12/31/2013 | 1/3/2014 1/7/2014 | 1/7/2014 | 12/31/2013 | 1/6/2014 1/8/2014 | 1/8/2014 | 12/31/2013 | 1/7/2014 1/9/2014 | 1/9/2014 | 12/31/2013 | 1/8/2014 1/10/2014 | 1/10/2014 | 12/31/2013 | 1/9/2014 1/11/2014 | | 12/31/2013 | 1/14/2014 1/12/2014 | | 12/31/2013 | 1/14/2014 1/13/2014 | 1/13/2014 | 12/31/2013 | 1/10/2014 1/14/2014 | 1/14/2014 | 12/31/2013 | 1/13/2014
任何建議
這兩種解決方案都可以工作,但是你的短小。謝謝!! –