sql >> Database teknologi >  >> RDS >> Sqlserver

SQL Server:Tæl poster (inklusive nul) pr. ti minutters intervaller

-- Fordi dine eksempeldata strækker sig over 27 timer:

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;


  1. WinDeath på notifyDataSetChanged()

  2. hvilken forespørgsel er bedre og effektiv - mysql

  3. Standard rækkefølge for udvalgt forespørgsel i oracle

  4. Oracle Installation produktmappe og diag mappe på et andet sted