1
Friends, good afternoon!
I came across a problem at work today, and I still haven’t found a solution to it. As I do not have much experience in the database area I am having difficulty to solve this problem, I would be very happy with a help, tip etc...
I have the following problem: I have a table where records are updated daily, but not all Ids have daily updated records.
I need that when one there is no next date repeat the previous example:
CREATE TABLE #CADASTRO ( ID INT IDENTITY, DATA_ATUALIZACAO DATE, VALOR INT)
INSERT INTO #CADASTRO VALUES ('20170201',1), ('20170204',4), ('20170205',5)
DECLARE @DATAINICIAL DATE = '20170201';
DECLARE @DATAFINAL DATE = '20170205';
;
WITH CTE AS
(
SELECT
@DATAINICIAL AS DATA_GUIA
/**********/UNION ALL/**********/
SELECT
DATEADD(DAY, 1, DATA_GUIA)
FROM CTE
WHERE DATA_GUIA < @DATAFINAL
)
SELECT
B.DATA_GUIA,
A.*
FROM #CADASTRO AS A
RIGHT JOIN CTE AS B ON A.DATA_ATUALIZACAO = B.DATA_GUIA
puts the query you are using and the table structure
– Rovann Linhalis
I put the example in question to keep it simple.
– Diego Soares
You should edit your question to add more information, not publish an answer to it.
– Rovann Linhalis