?
Solved

SQL 2012 Last time within in hour for a truck

Posted on 2013-11-12
4
Medium Priority
?
334 Views
Last Modified: 2013-11-12
I need to display the last time a truck checked in within the last hour.  I have a query which can pull all time values for the last hour.

SELECT [Vehicle_ID]
      ,[Day]
      ,[Month]
      ,[Year]
      ,[GPSTime]
      ,[Date_Time]
      ,[PC_Date]
      ,[PC_Time]
      ,[Latitude]
      ,[Longitude]
      ,[Speed]
      ,[Course]
      ,dateadd(second,GPSTime,PC_date) as GPS_Time
        ,geography::STPointFromText('POINT(' + CAST(Longitude AS VARCHAR(20)) + ' ' + CAST(Latitude AS VARCHAR(20)) + ')', 4326) AS GEOM

 
  FROM [Integrator].[dbo].[VehicleState]

  WHERE LATITUDE > 0 AND dateadd(second,GPSTime,PC_date) >= dateadd(hh, -1, getdate())

  GROUP BY [Vehicle_ID]
      ,[Day]
      ,[Month]
      ,[Year]
      ,[GPSTime]
      ,[Date_Time]
      ,[PC_Date]
      ,[PC_Time]
      ,[Latitude]
      ,[Longitude]
      ,[Speed]
      ,[Course]
      ,dateadd(second,GPSTime,PC_date)

Output:
Vehicle_ID      Day      Month      Year      GPSTime      Date_Time      PC_Date      PC_Time      Latitude      Longitude      Speed      Course      GPS_Time      GEOM
2501501      12      11      2013      37858      1384273858      2013-11-12      10:31:00      42.1915321350098      -96.7382354736328      0      NULL      2013-11-12 10:30:58.000      0xE6100000010C8D976E1283184540107A36AB3E2F58C0
2501504      12      11      2013      37824      1384273824      2013-11-12      10:30:26      42.1916656494141      -96.7380676269531      0      NULL      2013-11-12 10:30:24.000      0xE6100000010C545227A0891845405E4BC8073D2F58C0
2501514      12      11      2013      38058      1384274058      2013-11-12      10:34:21      42.2392654418945      -97.0178527832031      32.3369026184082      180      2013-11-12 10:34:18.000      0xE6100000010C66F7E461A11E45404CA60A46254158C0
2501522      12      11      2013      37810      1384273810      2013-11-12      10:30:12      42.2957153320313      -96.9594192504883      0      NULL      2013-11-12 10:30:10.000      0xE6100000010CAEB6627FD925454013F241CF663D58C0
2501527      12      11      2013      37741      1384273741      2013-11-12      10:29:04      42.2957496643066      -96.9596176147461      0      NULL      2013-11-12 10:29:01.000      0xE6100000010CAEB6627FD9254540764F1E166A3D58C0
2501527      12      11      2013      37775      1384273775      2013-11-12      10:29:38      42.2957496643066      -96.9596328735352      0      NULL      2013-11-12 10:29:35.000      0xE6100000010CAEB6627FD9254540764F1E166A3D58C0

As you can see truck 2501527 has check in twice in the last hour, so each one comes up in the query.  I would like it to just display the last time within in an hour for a truck.
0
Comment
Question by:PeelSeel2
  • 2
4 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 39642150
TOP

Open in new window


sorry on mobile check in BOL TOP
0
 
LVL 32

Accepted Solution

by:
awking00 earned 2000 total points
ID: 39642436
SELECT [Vehicle_ID]
       ,[Day]
       ,[Month]
       ,[Year]
       ,[GPSTime]
       ,[Date_Time]
       ,[PC_Date]
       ,[PC_Time]
       ,[Latitude]
       ,[Longitude]
       ,[Speed]
       ,[Course]
       ,GPS_Time
       ,GEOM
FROM
(SELECT [Vehicle_ID]
       ,[Day]
       ,[Month]
       ,[Year]
       ,[GPSTime]
       ,[Date_Time]
       ,[PC_Date]
       ,[PC_Time]
       ,[Latitude]
       ,[Longitude]
       ,[Speed]
       ,[Course]
       ,dateadd(second,GPSTime,PC_date) as GPS_Time
       ,geography::STPointFromText('POINT(' + CAST(Longitude AS VARCHAR(20)) + ' ' + CAST(Latitude AS VARCHAR(20)) + ')', 4326) AS GEOM,
row_number() over (partition by Vehicle_ID order by PC_Date, PC_Time desc) rn
FROM [Integrator].[dbo].[VehicleState]
WHERE LATITUDE > 0 AND dateadd(second,GPSTime,PC_date) >= dateadd(hh, -1, getdate())) as x
WHERE x.rn = 1;
0
 

Author Closing Comment

by:PeelSeel2
ID: 39642554
Worked great!!  Thanks!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39642601
Glad to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question