Solved

Sql query to last records from table

Posted on 2016-10-05
3
59 Views
Last Modified: 2016-10-08
I have sql table with these columns
CrewID
Lat
Lon
DateCreated

Now this table stores Latitude and Longitude of each crew when he travels along with Datetime when the position is recorded.

I need to write a query which will give me the last recorded position of each crew from this table bases on DateCreated.
0
Comment
Question by:yadavdep
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41830134
<Knee jerk reaction.  Write a subquery to get the most recent DateCreated for each CrewID, then join on the table as a main query.  Change YourTable and aliases to meet your needs. > 

SELECT yt.CrewID, yt.Lat, yt.Lon, yt.DateCreated
FROM YourTable yt
   JOIN (
      SELECT CrewID, Max(DateCreated) as DateCreatedMax
      FROM YourTable
      GROUP BY CrewID) ytmax ON yt.CrewID = ytmax.CrewID AND yt.DateCreated = ytmax.DateCreatedMax
ORDER BY yt.CrewID

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41830605
SELECT CrewID, Lat, Lon, DateCreated
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY CrewID ORDER BY DateCreated DESC) AS row_num
    FROM table_name
) AS derived
WHERE row_num = 1
0
 
LVL 23

Expert Comment

by:Pawan Kumar
ID: 41830978
SELECT x.CrewID, x.Lat, x.Lon, n.DateCreated
FROM YourTable x
CROSS APPLY
(
        SELECT MAX(y.DateCreated) DateCreated
	FROM YourTable y
	WHERE x.CrewID = y.CrewID    
)n

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

943 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

4 Experts available now in Live!

Get 1:1 Help Now