Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Sql Server:Hitung catatan (termasuk nol) per interval sepuluh menit

-- Karena data sampel Anda mencakup 27 jam:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apakah ada perbedaan antara SQL Server Express (2012) dan LocalDB?

  2. SQL Server Compact Edition ISNULL(sth, ' ') mengembalikan nilai boolean?

  3. Bagaimana cara menggabungkan dua tabel yang tidak terkait di sql

  4. mengisolasi sub-string dalam string sebelum simbol di SQL Server 2008

  5. Crystal Reports vs. Layanan Pelaporan Microsoft SQL Server