Solved

SQL - Get the latest records

Posted on 2013-11-12
9
699 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 45
SQL SELECT query help 7 41
SQL Server - Set Field Values ito Zero Based on Related Table 4 28
SQL Query Help Top 1 and Distinct? 6 26
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. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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