Solved

SQL 2012 Last time within in hour for a truck

Posted on 2013-11-12
4
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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 32

Expert Comment

by:awking00
ID: 39642601
Glad to help.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

752 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