Solved

SQL - Get the latest records

Posted on 2013-11-12
9
707 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:tommym121
ID: 39643663
My timestamp is generated using the GetDate()
0
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

828 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