按时间间隔分组10分钟

时间:2018-11-15 12:01:24

标签: sql-server-2008

嗨,我需要从一张表中获取纬度和经度的前10条记录,但是在ms sql 2012中需要间隔10分钟。 在我的表中有一列currentime(数据类型为datetime),它将以20到30秒的时间间隔插入,我应该做的只是通过分割时间来获取前10条记录

image of the record

因此在此图像中插入记录的时间间隔少于一分钟,我应在lat和long的第一个记录时间(2018-11-15 14:55:45.000)之前得到记录,第二个记录应为(2018 -11-15 14:45:45.000),第三条记录应该是(2018-11-15 14:35:45.000),依此类推。

到目前为止,我尝试使用datediff和许多可能的方法,但是找不到合适的方法   解决方案

     select  [Latitude]
     ,[Longitude] ,cast([Currenttime] as nvarchar(50)) as [Currenttime] from 
      VT_LocationHistory where VehicleId = '1' 
       GROUP BY
       DATEPART(YEAR, [Currenttime]),
       DATEPART(MONTH, [Currenttime]),
      DATEPART(DAY, [Currenttime]),
       DATEPART(HOUR, [Currenttime]),
        (DATEPART(MINUTE, [Currenttime])/10),[Latitude],[Longitude], 
      [Currenttime]
          order by ID desc






     WITH CTE AS (
     SELECT Currenttime, ROW_NUMBER() OVER (ORDER BY Currenttime DESC) RN
      FROM [VT_LocationHistory]
       )
       SELECT P.Currenttime, 
      P2.Currenttime as Currenttime, 
      DATEDIFF(mi,  P.Currenttime, P2.Currenttime)
      FROM CTE P
      JOIN CTE P2 ON P.RN = P2.RN + 1
      WHERE DATEDIFF(mi, P.Currenttime, P2.Currenttime) > 10



           select  id,Latitude,Longitude,Currenttime   from 
              [VT_LocationHistory] 
                group by datediff(HOUR, '1990-01-01T00:00:00', Currenttime) 
         /100000,id,Latitude,Longitude,Currenttime
         order by Currenttime desc



          select 
       [5min]  = dateadd(minute,(datediff(minute,0,a.dt)/5)*5,0),

       a.dt
         from
         (select dt = Currenttime from  VT_LocationHistory ) a 



     select  id,Latitude,Longitude,Currenttime   from [VT_LocationHistory] 
   GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, Currenttime) / 10 * 10, 
       0),id,Latitude,Longitude,Currenttime
      order by id desc


            SELECT top 10  DATEADD(MINUTE, DATEDIFF(MINUTE, 0, 
           aa.Currenttime) / 10 * 10, 0) 
         AS [date_truncated]
         FROM     VT_LocationHistory AS aa
         GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, aa.Currenttime) / 10 * 
         10, 
        0),Currenttime
        ORDER BY Currenttime desc

一切都白费了,所以请任何人都可以帮我找出我在做什么错。谢谢

0 个答案:

没有答案