SQL Server查询使用非空的下一行值填充空值

时间:2017-01-30 10:06:05

标签: sql-server

我有一个像这样的SQL Server查询:

select 
    CONVERT(datetime,tglolah,103) as dates,
    pks,asal,
    tandan,tbs_diolah,cpo,pko,
    CAST(NULLIF(cpo,0) as float)  / (NULLIF(tbs_diolah, 0)) as rend_ms,
from 
    daily_guu
where 
    year(CONVERT(datetime,tglolah, 103)) = 2017
    and month(CONVERT(datetime, tglolah, 103)) = 1
    and pks = 11

和此表中的结果

|   Dates  | PKS | Asal | Tandan | tbs_diolah |  cpo  |  pko | Rend_ms
|:---------|-----|------|--------|------------|------ |------|--------
| 2017-1-1 | 11  | 58   |    0   |     0      |  0    |   0  | null
| 2017-1-2 | 11  | 58   | 100460 |     0      |  0    |   0  | null
| 2017-1-3 | 11  | 58   | 136100 |     0      |  0    |   0  | null      
| 2017-1-16| 11  | 18   | 179330 |     0      |  0    |   0  | null
| 2017-1-16| 11  | 19   | 279990 |  293820    | 69633 |12933 | 0.236
| 2017-1-20| 11  | 40   | 298530 |  563390    |125805 |25973 | 0.223

如果tandan列不为零或为空但tbs_diolahcpo为空或零,我如何用最近的有值的下一个值填充rend_ms值?

在表格中,我希望日期2,3和16月的rend_ms值填充1月16日的值0.236。我缺乏SQL Server的经验。提前致谢

0 个答案:

没有答案