Friends, good afternoon!
I came across a problem at work today, and I have not yet got a solution for it. As I do not have much experience in the database area I am having difficulty solving this problem, I would be very happy with a help, hint etc ...
I have the following problem: I have a table where records are updated daily, but not all IDs have records updated daily.
I need that when there is no next date repeat the previous one 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