2016-02-02 94 views
1

我知道有很多解決方案,但不幸的是我無法使用分區或關鍵字TOP。沒有什麼我在早期的帖子上嘗試過。如何從前一行獲取最後一個非空值

我的表看起來像這樣: enter image description here

我想要的結果是,當任何完工百分比爲NULL,它應該從最後一個非值完成百分比值,如:

enter image description here

回答

2

這是使用outer apply最容易實現:

select t.projectname, t.sequence, 
     coalesce(t.completion_percentage, t2.completion_percentage) as completion_percentage 
from t outer apply 
    (select top 1 t2.* 
     from t t2 
     where t2.projectname = t.projectname and 
      t2.completion_percentage is null and 
      t2.sequence < t.sequence 
     order by t2.sequence desc 
    ) t2; 
0

你可以試試這個:

SELECT t2.project_name,t2.sequence, 
case when percentage is null then (select percentage from table1 t1 where t1.sequence=t2.sequence-1) 
else t2.percentage end as percentage 
FROM table t2 
相關問題