Question response time on StackOverflow
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('Comments') AND name = 'IX_CreationDate')
CREATE NONCLUSTERED INDEX IX_CreationDate
ON dbo.Comments(CreationDate)
GO


IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('Posts') AND name = 'IX_PostID_CreationDate_Title')
CREATE NONCLUSTERED INDEX IX_PostID_CreationDate_Title
ON dbo.Posts(Id, CreationDate,Title)
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('Comments') AND name = 'IX_PostID_Text')
CREATE NONCLUSTERED INDEX IX_PostID_Text
ON dbo.Comments(PostId)
INCLUDE(Text)
GO


WITH BASE AS
(


SELECT 
    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 0 AND 59 ) AS WithinHour,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 60 AND 1440) AS WithinDay,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 1441 AND 10080) AS WithinWeek,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 10081 AND 44640) AS WithinMonth,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 44641 AND 132480) AS Within3Month,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 132481 AND 525600) AS WithinYear,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) BETWEEN 525601 AND 7884000) AS OverAYear,

    (SELECT COUNT(*)
    FROM Posts T1
    LEFT JOIN Comments T2 ON T1.Id = T2.PostId
    WHERE T2.CreationDate = (SELECT MIN(T3.CreationDate)
                                FROM Comments T3
                                WHERE T3.PostId = T1.Id)
    AND DATEDIFF(minute, T1.CreationDate, T2.CreationDate) &gt; -1) AS Total
)

,

ResponseTime AS (

SELECT 
    WithinHour,
    CAST(CAST(WithinHour AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS WithinHourPerc,
    WithinDay,
    CAST(CAST(WithinDay AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS WithinDayPerc,
    WithinWeek,
    CAST(CAST(WithinWeek AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS WithinWeekPerc,
    WithinMonth,
    CAST(CAST(WithinMonth AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS WithinMonthPerc,
    Within3Month,
    CAST(CAST(Within3Month AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS Within3MonthPerc,
    WithinYear,
    CAST(CAST(WithinYear AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS WithinYearPerc,
    OverAYear,
    CAST(CAST(OverAYear AS numeric(10,2)) / CAST(Total AS numeric(10,2)) * 100 AS numeric(5,2)) AS OverAYearPerc,
    Total

FROM BASE

)

SELECT *
FROM ResponseTime