Usuário com melhor resposta
Dúvida: Urgente! Qtd / Vlr do último registro não Nulo

Pergunta
-
Pessoal,
Imaginem uma tabela fake como a de baixo com as colunas de DATA, CODIGO e QTD
como eu faço uma segunda coluna de valores (como a em amarelo) que repita sempre o último valor não nulo de datas passadas?
Lembrando que tenho neste exemplo dois produtos diferentes....
Obrigada
abs
"Se esta sugestão for útil, por favor, classifique-a como útil.
Se ela lhe ajudar a resolver o problema, por favor, marque-a como Resposta."
Érica Tohoma | http://bloggirlsa.wordpress.com
Respostas
-
Boa tarde,
Experimente mais ou menos dessa forma:
select t.data, t.codigo, t.qtd, coalesce (t.qtd, (select top 1 a.qtd from Tabela as a where a.codigo = t.codigo and a.data < t.data and a.qtd is not null
order by a.data desc)) as qtd_nao_null from Tabela as t
Espero que ajude.
Assinatura: http://www.imoveisemexposicao.com.br
- Editado gapimex quarta-feira, 14 de agosto de 2013 20:32
- Sugerido como Resposta Alexandre Matayosi quarta-feira, 14 de agosto de 2013 21:21
- Marcado como Resposta éricat quinta-feira, 15 de agosto de 2013 11:55
-
Erica, agora sim, testado e funcionando:
Create table #Fake (data int, codigo char(1), qtd int) insert into #Fake values (20130626, 'A', -72), (20130627, 'A', -72), (20130628, 'A', NULL), (20130629, 'A', NULL), (20130630, 'A', 237), (20130701, 'A', NULL), (20130626, 'B', 265), (20130627, 'B', 265), (20130628, 'B', NULL), (20130629, 'B', NULL), (20130630, 'B', 0), (20130701, 'b', NULL) alter table #fake add Desejado int update #Fake set desejado = qtd declare @data int declare @codigo char(1) While (select COUNT(*) from #Fake where desejado is null) > 0 begin set @data = (select min(DATA) from #Fake where desejado is null) set @codigo = (select top 1 codigo from #Fake where DATA = @data and desejado is null) update #Fake set desejado = (select top 1 QTD from #Fake where data = (select MAX(data) from #Fake where data < @data and codigo = @codigo and qtd is not null) and codigo = @codigo and qtd is not null) where data = @data and codigo = @codigo and desejado is null end select * from #Fake
Alexandre Matayosi Conde Mauricio.
- Sugerido como Resposta Alexandre Matayosi quarta-feira, 14 de agosto de 2013 21:21
- Marcado como Resposta éricat quinta-feira, 15 de agosto de 2013 11:55
Todas as Respostas
-
Boa tarde,
Experimente mais ou menos dessa forma:
select t.data, t.codigo, t.qtd, coalesce (t.qtd, (select top 1 a.qtd from Tabela as a where a.codigo = t.codigo and a.data < t.data and a.qtd is not null
order by a.data desc)) as qtd_nao_null from Tabela as t
Espero que ajude.
Assinatura: http://www.imoveisemexposicao.com.br
- Editado gapimex quarta-feira, 14 de agosto de 2013 20:32
- Sugerido como Resposta Alexandre Matayosi quarta-feira, 14 de agosto de 2013 21:21
- Marcado como Resposta éricat quinta-feira, 15 de agosto de 2013 11:55
-
Obrigada por responder,
mas não deu certo....
create table #tabela (data date ,codigo varchar(1) ,qtd int) insert into #tabela values ('20130626', 'a', -72) , ('20130627', 'a', -72) , ('20130628', 'a', NULL) , ('20130629', 'a', NULL) , ('20130630', 'a', 237) , ('20130701', 'a', NULL) , ('20130702', 'a', -73) , ('20130703', 'a', NULL) , ('20130704', 'a', -73) , ('20130626', 'b', -265) , ('20130627', 'b', -265) , ('20130628', 'b', NULL) , ('20130629', 'b', NULL) , ('20130630', 'b', 0) , ('20130701', 'b', NULL) , ('20130702', 'b', -265) , ('20130703', 'b', NULL) , ('20130704', 'b', -265) select t.data, t.codigo, t.qtd, coalesce (t.qtd, (select a.qtd from #tabela as a where a.codigo = t.codigo and a.data < t.data and a.qtd is not null)) as qtd_nao_null from #tabela as t
"Se esta sugestão for útil, por favor, classifique-a como útil.
Se ela lhe ajudar a resolver o problema, por favor, marque-a como Resposta."
Érica Tohoma | http://bloggirlsa.wordpress.com -
Érica, de uma olhada no exemplo abaixo, trabalhando com temporaria e lendo os nulos em um looping, ainda não esta 100% mas esta quase:
Create table #Fake (data int, codigo char(1), qtd int) insert into #Fake values (20130626, 'A', -72), (20130627, 'A', -72), (20130628, 'A', NULL), (20130629, 'A', NULL), (20130630, 'A', 237), (20130701, 'A', NULL), (20130626, 'B', 265), (20130627, 'B', 265), (20130628, 'B', NULL), (20130629, 'B', NULL), (20130630, 'B', 0), (20130701, 'b', NULL) alter table #fake add Desejado int update #Fake set desejado = qtd declare @data int declare @codigo char(1) While (select COUNT(*) from #Fake where desejado is null) > 0 begin set @data = (select top 1 DATA from #Fake where desejado is null) set @codigo = (select top 1 codigo from #Fake where DATA = @data and desejado is null) update #Fake set desejado = (select top 1 QTD from #Fake where data = (select MAX(data) from #Fake where data < @data and codigo = @codigo and qtd is not null)) where data = @data and codigo = @codigo and desejado is null end
Alexandre Matayosi Conde Mauricio.
-
Érica,
Percebi, logo depois que postei, que a subquery estava com problemas e editei aquele trecho para adicionar o Top 1 e o Order by.
Experimente fazer um novo teste com essa versão.
Assinatura: http://www.imoveisemexposicao.com.br
-
Erica, agora sim, testado e funcionando:
Create table #Fake (data int, codigo char(1), qtd int) insert into #Fake values (20130626, 'A', -72), (20130627, 'A', -72), (20130628, 'A', NULL), (20130629, 'A', NULL), (20130630, 'A', 237), (20130701, 'A', NULL), (20130626, 'B', 265), (20130627, 'B', 265), (20130628, 'B', NULL), (20130629, 'B', NULL), (20130630, 'B', 0), (20130701, 'b', NULL) alter table #fake add Desejado int update #Fake set desejado = qtd declare @data int declare @codigo char(1) While (select COUNT(*) from #Fake where desejado is null) > 0 begin set @data = (select min(DATA) from #Fake where desejado is null) set @codigo = (select top 1 codigo from #Fake where DATA = @data and desejado is null) update #Fake set desejado = (select top 1 QTD from #Fake where data = (select MAX(data) from #Fake where data < @data and codigo = @codigo and qtd is not null) and codigo = @codigo and qtd is not null) where data = @data and codigo = @codigo and desejado is null end select * from #Fake
Alexandre Matayosi Conde Mauricio.
- Sugerido como Resposta Alexandre Matayosi quarta-feira, 14 de agosto de 2013 21:21
- Marcado como Resposta éricat quinta-feira, 15 de agosto de 2013 11:55