Solved

SQL 2012 Last time within in hour for a truck

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

623 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