Solved

SQL 2012 Last time within in hour for a truck

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select JOIN table 2 19
SQL Exceptions 3 37
SSRS: My Data Source no longer has any dependent items ??? 2 14
SQL Pivot add row totals 2 12
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

21 Experts available now in Live!

Get 1:1 Help Now