- Vì dữ liệu mẫu của bạn kéo dài 27 giờ:
DECLARE
@hours TINYINT,
@minute_interval TINYINT,
@start SMALLDATETIME;
SELECT
@hours = 27,
@minute_interval = 10,
@start = '20100122 13:00';
;WITH x AS
(
SELECT TOP (@hours * (60 / @minute_interval))
n = ROW_NUMBER() OVER
(ORDER BY column_id)
FROM msdb.sys.columns
),
intervals(boundary) AS
(
SELECT CONVERT
(
SMALLDATETIME,
DATEADD(MINUTE, (-n * @minute_interval), @start)
)
FROM x
)
SELECT
i.boundary,
RequestCount = COUNT(d.id),
DistinctPopIDs = COUNT(DISTINCT d.popid)
FROM
intervals AS i
LEFT OUTER JOIN
dbo.Track_PageSubmit AS d
ON d.requestBegin >= i.boundary
AND d.requestBegin < DATEADD(MINUTE, @minute_interval, i.boundary)
GROUP BY i.boundary
ORDER BY i.boundary;