• I suggest making your code as self-documenting as possible; then you need fewer additional comments. Naturally that means avoiding variable "names" like "E", "N" and so on. Example for this code below.

    Of course you may prefer to adjust the CROSS JOINs for efficiency and/or adjust the names, but make the names meaningful. Because tally tables are quite common, I keep the first two names the same, but the final use of the tally table should have a column name that identifies specifically what the data is.

    ;WITH

    cteDigits AS (

    SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL

    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9

    ),

    cteTally AS (

    SELECT

    [1000000s].digit * 1000000 + [100000s].digit * 100000 + [10000s].digit * 10000 +

    [1000s].digit * 1000 + [100s].digit * 100 + [10s].digit * 10 + [1s].digit AS minutes_increment

    FROM cteDigits [1s]

    CROSS JOIN cteDigits [10s]

    CROSS JOIN cteDigits [100s]

    CROSS JOIN cteDigits [1000s]

    CROSS JOIN cteDigits [10000s]

    CROSS JOIN cteDigits [100000s]

    CROSS JOIN cteDigits [1000000s]

    WHERE

    [1000000s].digit * 1000000 + [100000s].digit * 100000 + [10000s].digit * 10000 +

    [1000s].digit * 1000 + [100s].digit * 100 + [10s].digit * 10 + [1s].digit

    --limit rows generated to 6 yrs' worth of minutes

    <= DATEDIFF(MINUTE, '19000101', '19060101')

    ),

    cteTime_by_minute AS (

    SELECT DATEADD(MINUTE, minutes_increment, DATEADD(YEAR, -2, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, GETDATE()), 0)))

    AS time_by_minute

    FROM cteTally t

    )

    SELECT time_by_minute

    INTO Sandbox.dbo.Time

    FROM cteTime_by_minute

    Edit: Added "INTO Sandbox.dbo.Time".

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.