Data extrapolation on the fly

,

In some cases we need to generate the data based on a starting date, to an end data (today-getdate()). This process is called data extrapolation based on limited information (starting date).

Here I am trying to achieve the same without storing data in temp./staging tables.

First I am creating a temp. table wih 2 rows and now I need to generate rest of the rows starting from given date to today's date.

Next step is to make use of CTE to generate sequence of rows and finally making use of CROSS JOIN to join the sequence with real data and get the desired output.

Thanks
Mohit Nayyar

CREATE TABLE #temp
(
myName VARCHAR (10),
StartDate DateTime
)
GO

INSERT INTO #temp VALUES ('Mohit', '2008-09-15')
INSERT INTO #temp VALUES ('Nayyar', '2008-09-22')
GO

WITH First2 (seq) AS
(
SELECT 1 UNION ALL SELECT 1
)
, First4 (seq) AS
(
SELECT 1 FROM First2 x CROSS JOIN First2 y
)
, First16 (seq) AS
(
SELECT 1 FROM First4 x CROSS JOIN First4 y
)
, First256 (seq) AS
(
SELECT 1 FROM First16 x CROSS JOIN First16 y
)
, First65536 (seq) AS
(
SELECT 1 FROM First256 x CROSS JOIN First256 y
)
, SeqRows AS
(
SELECT ROW_NUMBER () OVER (Order by Seq) AS SeqNumber
FROM First65536 WITH (NOEXPAND)
)

SELECT myName, StartDate, 
DATEADD(DAY, n.SeqNumber-1, StartDate) AS Extrapolation
FROM #temp
CROSS JOIN SeqRows n
WHERE n.SeqNumber <= DATEDIFF(DAY, StartDate, GETDATE())
ORDER BY myName, StartDate, 3
GO

DROP TABLE #temp
GO

Rate

3.5 (2)

Share

Share

Rate

3.5 (2)