How to fill null with value from the previous line?

2

Looking at the following table, I want to fill in the value that is null with the value of the previous line, what is the best thing to do with mysql?

    
asked by anonymous 21.10.2015 / 16:03

1 answer

2

Considering that the rows are ordered by column header1, and that this column is numeric and sequential (no numbering holes):

select  cabecalho1, cabecalho2, 
        novoresultado = isnull(cabecalho2,(select cabecalho2 from yourTable where cabecalho1= t.cabecalho1-1))
from yourTable t

--update
update yourTable
set novoresultado = isnull(cabecalho2,(select cabecalho2 from yourTable t where t.cabecalho1 = yourTable.cabecalho1-1))

Considering that the rows are ordered by column header1, and that this column is numeric, but with the possibility of missing numbers in the sequence:

select  cabecalho1, cabecalho2, 
        novoresultado = isnull(cabecalho2,(select top 1 cabecalho2 from yourTable where cabecalho1 < t.cabecalho1 order by cabecalho1 desc))
from yourTable t

--update
update yourTable
set novoresultado = isnull(cabecalho2,(select top 1 cabecalho2 from yourTable t where t.cabecalho1 < yourTable.cabecalho1 order by cabecalho1 desc))
    
28.10.2016 / 13:06