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

SQL Server gruppere efter tælling af dato og tid pr. time?

Hvad med dette? Forudsat SQL Server 2008:

SELECT CAST(StartDate as date) AS ForDate,
       DATEPART(hour,StartDate) AS OnHour,
       COUNT(*) AS Totals
FROM #Events
GROUP BY CAST(StartDate as date),
       DATEPART(hour,StartDate)

For før 2008:

SELECT DATEADD(day,datediff(day,0,StartDate),0)   AS ForDate,
       DATEPART(hour,StartDate) AS OnHour,
       COUNT(*) AS Totals
FROM #Events
GROUP BY CAST(StartDate as date),
       DATEPART(hour,StartDate)

Dette resulterer i:

ForDate                 | OnHour | Totals
-----------------------------------------
2011-08-09 00:00:00.000     12       3


  1. NLS_CHARSET_NAME() Funktion i Oracle

  2. SQL Server-markørtyper - Hvad er statiske markører i SQL Server | SQL Server Tutorial / TSQL Tutorial

  3. Introduktion til PL/SQL-pakker i Oracle-databasen

  4. dødvande i Oracle