Olhando para a seguinte tabela, quero preencher o valor que estiver nulo com o valor da linha anterior, qual a melhor de o fazer com mysql?
1 Resposta
Considerando que as linhas estão ordenadas pela coluna cabecalho1, e que esta coluna é numérica e sequencial (sem buracos na numeração):
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))
Considerando que as linhas estão ordenadas pela coluna cabecalho1, e que esta coluna é numérica, mas com a possibilidade de existirem números em falta na sequência:
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))