Solved

SQL 2012 Last time within in hour for a truck

Posted on 2013-11-12
4
306 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 31

Accepted Solution

by:
awking00 earned 500 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 31

Expert Comment

by:awking00
ID: 39642601
Glad to help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now