如何使用时间函数获取每小时数据

时间:2014-03-18 16:17:44

标签: sql

我试图获取一个表上的每小时数据,该表在mysql上运行正常但在sql上运行不正常。如何在sql server managmenmt studio中使用它,我收到' time'不是公认的函数名称。而curdate()不识别函数。

  select  [pa_number], [pa_surname]
      ,[pa_forename],
        sum(time(datetime) >= '07:00:00' and time(datetime) < '08:00:00') as '7.00-8.00 AM',  
       sum(time(datetime) >= '08:00:00' and time(datetime) < '09:00:00') as '8.00-9.00 AM',  
       sum(time(datetime) >= '09:00:00' and time(datetime) < '10:00:00') as '9.00-10.00 AM ',  
       sum(time(datetime) >= '10:00:00' and time(datetime) < '11:00:00') as '10.00-11.00 AM',   
       sum(time(datetime) >= '11:00:00' and time(datetime) < '12:00:00') as '11.00-12.00 AM',   
   sum(time(datetime) >= '12:00:00' and time(datetime) < '13:00:00') as '12.00-1.00 PM',   
   sum(time(datetime) >= '13:00:00' and time(datetime) < '14:00:00') as '1.00-2.00 PM',   
   sum(time(datetime) >= '14:00:00' and time(datetime) < '15:00:00') as '2.00-3.00 PM',  
   sum(time(datetime) >= '15:00:00' and time(datetime) < '16:00:00') as '3.00-4.00 PM',   
   sum(time(datetime) >= '16:00:00' and time(datetime) < '17:00:00') as '4.00-5.00 PM',  
   sum(time(datetime) >= '17:00:00' and time(datetime) < '18:00:00') as '5.00-6.00 PM',  
   sum(time(datetime) >= '18:00:00' and time(datetime) < '19:00:00') as '6.00-7.00 PM'   



 from [ICPS].[dbo].[parking_attendants] t  
   inner join tickets a  
   on t.[pa_number] = a.[t_pa_number]  

  where cast(t.DATETIME AS DATE) = CURDATE()  
  group by pa_surname 

1 个答案:

答案 0 :(得分:0)

您可以在第一部分使用SUM(DATEPART(HOUR, t.datetime) BETWEEN 7 AND 8)等,第二部分使用CAST(GETDATE() AS DATE)

DATEPARTBETWEENGETDATE

的MS参考