generate sql temp table of sequential dates to left outer join to

前端 未结 3 1289
有刺的猬
有刺的猬 2020-12-18 05:34

i have a table of data that i want to select out via stored proc such that users can connect a MS excel front end to it and use the raw data as a source to graph.

T

3条回答
  •  执念已碎
    2020-12-18 06:24

    In SQL Server 2005 and up, you can use something like this (a Common Table Expression CTE) to do this:

    DECLARE @DateFrom DATETIME
    SET @DateFrom = '2011-01-01'
    
    DECLARE @DateTo DATETIME
    SET @DateTo = '2011-01-10'
    
    ;WITH DateRanges AS
    (
        SELECT @DateFrom AS 'DateValue'
        UNION ALL
        SELECT DATEADD(DAY, 1, DateValue)
        FROM DateRanges
        WHERE DateValue < @DateTo
    )
    SELECT * FROM DateRanges
    

    You could LEFT OUTER JOIN this CTE against your table and return the result.

提交回复
热议问题