Solved

SQL - Get the latest records

Posted on 2013-11-12
9
689 Views
Last Modified: 2013-11-13
I have a table with columns

TimeStamp
FirstName
LastName
EmployeeID (Primary Key)
LastInsuranceClaimAmount

I like to create a query to generate a table of the latest claim for each employee since Jan 1, 2013.  In the table there can be more than one records related to a EmployeeID. Records before Jan 1, 2013 will not be take into consideration.

The result should have the same column as the above table.

Can anyone give me an idea how to do that? Thanks.
0
Comment
Question by:tommym121
  • 3
  • 3
  • 3
9 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39643591
If I understood you correctly, this is what you need to do:

;WITH cte AS
(
Select EmployeeID,
[TimeStamp],
ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY [TimeStamp] DESC) rn
FROM Table1
)
Select t.[TimeStamp],
t.FirstName,
t.LastName,
t.EmployeeID,
t.LastInsuranceClaimAmount FROM Table1 t INNER JOIN CTE c ON c.EmployeeID=t.EmployeeID AND c.[TimeStamp] = t.[TimeStamp] AND c.rn=1
WHERE t.[TimeStamp] >= '20130101'

Open in new window

0
 
LVL 24

Accepted Solution

by:
chaau earned 350 total points
ID: 39643597
BTW, to create a table from SELECT statement, just add an INTO clause, like this:

;WITH cte AS
(
Select EmployeeID,
[TimeStamp],
ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY [TimeStamp] DESC) rn
FROM Table1
)
Select t.[TimeStamp],
t.FirstName,
t.LastName,
t.EmployeeID,
t.LastInsuranceClaimAmount 
INTO NewTable
FROM Table1 t INNER JOIN CTE c ON c.EmployeeID=t.EmployeeID AND c.[TimeStamp] = t.[TimeStamp] AND c.rn=1
WHERE t.[TimeStamp] >= '20130101'

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 150 total points
ID: 39643645
Note that in SQL Server a timestamp data type isn't actually related to dates or times
(and Microsoft now prefer then to be referred to a "rowversion")
The rowversion data type is just an incrementing number and does not preserve a date or a time. ...
This binary data type provides uniqueness for each record but isn't guaranteed to produce "the latest" (but probably will suffice for that).

However you can't use [timestamp] against a date in the where clause such as >= '20130101'

Can you confirm the actual data type is "timestamp"?
Do you have any other date/datetime fields on the table you can use?
0
 

Author Comment

by:tommym121
ID: 39643663
My timestamp is generated using the GetDate()
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39643673
ok then my comments don't apply,
but you probably should not use the word 'timestamp' as a fieldname :)
0
 
LVL 24

Expert Comment

by:chaau
ID: 39643730
That's why I have exposed it in brackets in my answer
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39643761
:) agreed, that certainly is needed.

But it's sort of worse than just using a reserved word because "timestamp" is currently so misleading in SQL Server Plus, at sometime in the future MS will adopt the standard meaning I believe - which will be a small nightmare in itself. Better IMHO to avoid using that term as a field name whenever possible.

time_stamp might be an alternative?
0
 

Author Comment

by:tommym121
ID: 39644205
PortierPaul, thanks to straighten me on terms.
0
 

Author Closing Comment

by:tommym121
ID: 39644207
Thank you
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.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

911 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