Solved

SQL - Get the latest records

Posted on 2013-11-12
9
683 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
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.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

707 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

16 Experts available now in Live!

Get 1:1 Help Now